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:
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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 56
- Comments: 40 (2 by maintainers)
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: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:
This will create
~/.huskyrc
file, which will add your node directory toPATH
. Some explanation to this command (as bash scripts usually look like black magic to front-end devs):which node
yields path from whichnode
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.
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.
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:
yarn
installed with homebrewThis was the error I was getting and this is the way I fixed and make it work:
Solution
~/.huskyrc
file in my home directoryThis 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: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:
This is an error. If I get this:
in Sourcetree, it’s doesn’t commit.
Has anyone figured out a solution?
My message is different
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:
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 withn
.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: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
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:
~/.bash_profile
: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
orwget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
install.sh
file and execute it to install3.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
then update
source .bash_profile
BTW: if you need to fix it immediately you can use your terminal to 'git commi’t your changes
my default is
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