husky: husky with sourcetree : Can't find node in PATH, trying to find a node binary on your system

I’m trying to use husky precommit with sourcetree. The precommit command is run but i get the following error:

image

Does anybody know what’s wrong? I’m using n.

OS: macOS Mojave 10.14 Node: 10.13 $ which node: /usr/local/bin/node npm: 6.4.1 $ whick npm: /usr/local/bin/npm

image

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 56
  • Comments: 40 (2 by maintainers)

Most upvoted comments

I was experiencing this error with NVM & WebStorm (while everything was fine from the Ubuntu terminal).

In a nutshell, it’s just a path problem with NPM & GUI vs terminal as @typicode suggests. In my case, I ended up configuring ~/.huskyrc as suggested in docs, to make sure it effectively uses NVM:

# ~/.huskyrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

I’ve spent some time figuring it out. Here’s a fix that should work in any situation, though I tested it only on MacOS Big Sur with node installed via nvm. But it should work no matter how you installed your node.

Issue

The thing is that SourceTree git hooks run within “clean” context and we need to add our node directory into that context. Husky enables that to us, as it runs ~/.huskyrc before executing any node-related commands. But we need to create this file first.

Solution

Run:

echo "export PATH=\"$(dirname $(which node)):\$PATH\"" > ~/.huskyrc

This will create ~/.huskyrc file, which will add your node directory to PATH. Some explanation to this command (as bash scripts usually look like black magic to front-end devs):

  • which node yields path from which node is being fired, in my case /Users/myusername/.nvm/versions/node/v15.4.0/bin/node
  • dirname $(which node) yields directory from that path, in my case /Users/myusername/.nvm/versions/node/v15.4.0/bin
  • echo "export PATH=\"$(dirname $(which node)):\$PATH\"" just prints out: export PATH="/Users/myusername/.nvm/versions/node/v15.4.0/bin:$PATH"
  • > ~/.huskyrc that part takes what has been printed out and saves it into a file ~/.huskyrc

It works for me. My machine is macOS mojave 10.14.6(18G2022) I use bash shell.

# ~/.huskyrc
export PATH="/usr/local/bin/:$PATH"
$ which node
/usr/local/bin/node
v13.8.0
$ which yarn
/usr/local/bin/yarn
1.22.0

I have the same problem, then I launched SourceTree from terminal, then it worked! First you need to make sure you have stree cli installed. image Then from your terminal run stree it works for me!

It’s actually not an error, just an information message. But it may sound like an error, especially as the output is red.

Node version managers works by modifying the PATH when the terminal is started. GUI clients usually don’t work well with version managers as they don’t source .bashrc or .zshrc where version managers are usually initialized.

So this message is just to let you know that husky will go into a mode where it’ll try to find a Node binary (it uses run-node for that).

Here’s more documentation https://github.com/typicode/husky/blob/master/DOCS.md#node-version-management

I opened a related issue on the SourceTree Jira: https://jira.atlassian.com/browse/SRCTREE-7184

Basically the same issue with the PATH not including /usr/local/bin. Please up vote.

After executing npx husky install .config/husky from the terminal; the errors resolved for me.

In my case:

  • Mac with Apple M1 chip
  • yarn installed with homebrew
  • Using NVM loaded as a Oh My Zsh plugin

This was the error I was getting and this is the way I fixed and make it work:

Screen Shot 2022-03-29 at 14 16 48

Solution

  • Create ~/.huskyrc file in my home directory
  • With this content:
# Load Yarn command coming from homebrew
export PATH="/opt/homebrew/bin/:$PATH"

# This loads nvm.sh and sets the correct PATH before running hook
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

nvm use default

This worked for me (and other Git apps that were having issues). This is on a Mac. # HUSKY_SKIP_HOOKS=1 open -a "SourceTree"

If you’re using macOS and NVM, then that’s the proper solution: just create .huskyrc in your ~ home directory with next content:

#!/usr/bin/env bash

source ~/.bash_profile

if test -f ".nvmrc"; then
    nvm use
fi

NOTE: it using your .bash_profile where NVM initialization should be already defined earlier when you installed NVM. I.e. next should be there already:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

This is an error. If I get this: image in Sourcetree, it’s doesn’t commit. Has anyone figured out a solution? My message is different

Can't find node in PATH, trying to find a node binary on your system
husky > pre-commit (node v6.9.2)
lint-staged requires at least version 8.6.0 of Node, please upgrade
husky > pre-commit hook failed (add --no-verify to bypass)

but I using nvm and I have node 8.11.3 running.

You need to set your system-wide node version to 8 or higher:

nvm alias default 8

After executing npx husky install .config/husky from the terminal; the errors resolved for me.

this worked for me 🕺

I found somewhat of a reason/solution for the issue with Sourcetree. All of my dependencies are current, node, husky, lint-staged and no solution works but this,

https://stackoverflow.com/questions/52754063/git-push-failed-due-to-husky-pre-push-on-sourcetree

@zhaozhe0831 I have same problem when I switch node version to 8.12.0 with n.

After executing npx husky install .config/husky from the terminal; the errors resolved for me.

This worked for me too.

None of the solutions worked for me. There’s a dirty fix, that worked for me. Open .git/hooks/pre-commit, put next code at the start of the file:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Closing as many solutions have been provided (thank you! 👍 )

See also https://typicode.github.io/husky/#/?id=command-not-found (works for v4 and v5)

This happens to me in Visual Studio Code if there’s an linting error in the code, which we take care of in the pre-commit hook using eslint. vscode will display

Info: can't find node in PATH, trying to find a node binary on your system

in a pop up, even though it isn’t the actual error, making it confusing for people to see what the real problem is (unless they check the output log).

If the commit goes through, there’s no popup. Does anyone have an idea how I can get vscode to display the actual error instead of this?

In my MAC, I fixed this by:

  1. Check Node path:
which node
/usr/local/bin/node
  1. Update ~/.bash_profile:
# ...
export PATH="/usr/local/bin:$PATH"

After executing npx husky install .config/husky from the terminal; the errors resolved for me.

this worked for me

After executing npx husky install .config/husky from the terminal; the errors resolved for me.

this worked for me 🕺

When I do that it bypasses the precommit. and it couldnt find node in terminal…

Hi, guys, I met the same issue, here is how I fix it.

1.firstly, this is about the Node path problem, I try to change my .bash_profile, but it does still not work, so I installed “nvm”

Two ways to install curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash or wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

  1. as mentioned above, if you can not install nvm, you can download the install.sh file and execute it to install

3.after finished step 3 you can input ‘nvm ls’ in your terminal if you can successfully use this command, you can skip stepping 5, if you still can not use ‘nvm command’, skip stepping 4

  1. add to your .bash_profile
export NVM_DIR="/Users/magicdawn/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

then update source .bash_profile

  1. in your terminal, then fix this issue nvm install v12

BTW: if you need to fix it immediately you can use your terminal to 'git commi’t your changes

my default is

default -> 8 (-> v8.11.3)

But I still get the same thing above when trying to commit with Sourcetree.

The problem appears to be related to using nvm and changing node versions since after I switched versions I was unable to commit anything, however, opening Sourcetree from the terminal works for me…

open /Applications/SourceTree.app/Contents/MacOS/SourceTree

I had the same issue with Webstorm. If anyone is using fnm, then the command to fix it would be:

fnm default 12.10.0