Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zhaozhe0831 opened this issue Oct 31, 2018 · 40 comments

Comments

@zhaozhe0831
Copy link

zhaozhe0831 commented Oct 31, 2018

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

@haithemT
Copy link

haithemT commented Nov 2, 2018

same issue here after upgrading node to v10.13

screen shot 2018-11-02 at 09 24 09

@zhaozhe0831
Copy link
Author

zhaozhe0831 commented Nov 8, 2018

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

@typicode
Copy link
Owner

typicode commented Nov 8, 2018

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

@mdversion01
Copy link

mdversion01 commented Mar 22, 2019

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.

@fabb
Copy link

fabb commented Mar 23, 2019

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

nvm alias default 8

@mdversion01
Copy link

my default is

default -> 8 (-> v8.11.3)

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

@mdversion01
Copy link

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

@thoqbk
Copy link

thoqbk commented Aug 29, 2019

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"

@bitclaw
Copy link

bitclaw commented Oct 17, 2019

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

@spacecakes
Copy link

spacecakes commented Oct 21, 2019

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?

@sainnr
Copy link

sainnr commented Oct 24, 2019

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"

@zkrige
Copy link

zkrige commented Oct 31, 2019

@sainnr solution worked for me on Catalina/zsh

@valeriopisapia
Copy link

The last one works for me as well

@crgeee
Copy link

crgeee commented Nov 12, 2019

thank you @sainnr

@mohammadalipak
Copy link

on my mac Mojave, I fixed this by:

Check Node path:

which node
/usr/local/bin/node

Add this path to the globals PATHs:

sudo vi /etc/paths.d/local_bin

enter edit mode by typing i then type this into the file:

/usr/local/bin

and save it esc wq enter

@crespowang
Copy link

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!

@alexilyaev
Copy link

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.

@ogelacinyc
Copy link

ogelacinyc commented Mar 10, 2020

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

@bobrosoft
Copy link

bobrosoft commented May 18, 2020

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"

@tenadolanter
Copy link

Switching to husky 3.1.0 (which uses run-node) also worked for me

#639

@doup
Copy link

doup commented Sep 3, 2020

In my case @sainnr solution (#390 (comment)) was almost there, I was missing nvm use after initializing NVM, like so:

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

@thelpbogeyman
Copy link

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

@tr1stan
Copy link

tr1stan commented Oct 15, 2020

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

This doesn't solve the issue though, it just stops the hooks from firing altogether.

+1 for downgrading to 3.1.0 so that team members who might use a GUI don't need extra setup.

@ShoshoWang
Copy link

ShoshoWang commented Oct 23, 2020

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

@bypatryk
Copy link

bypatryk commented Jan 18, 2021

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

@typicode
Copy link
Owner

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)

@Legys
Copy link

Legys commented Mar 8, 2021

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"

@tiagosiebler
Copy link

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"

Thank you! This worked to resolve this with sourcetree on Mac!

@appsparkler
Copy link

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

@k-utsumi k-utsumi mentioned this issue Jun 16, 2021
@kirillgroshkov
Copy link

This comment made it work for me! #390 (comment)

@daviddunndiscovery
Copy link

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

this worked for me 🕺

@luckyAisen
Copy link

work for me #390 (comment)

@kirillgroshkov
Copy link

BTW, SourceTree just released a new version on 2021-08-23 that fixes this issue for me! Now it works regardless if I open SourceTree via command line or via Application icon.

@wvddrss
Copy link

wvddrss commented Sep 10, 2021

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..

@Kaicha
Copy link

Kaicha commented Dec 2, 2021

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

This worked for me too.

@alexventuraio
Copy link

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

@drpicox
Copy link

drpicox commented Apr 5, 2022

None of the solutions work for me :(
(and I tried them all)

Screenshot 2022-04-05 at 20 00 23

Does anyone know from where takes the environment SourceTree?
Thanks!

BTW: command line $ git commit works correctly

@craigwheeler
Copy link

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

@luanzhuxian
Copy link

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

this worked for me

@ladytellur
Copy link

ladytellur commented May 21, 2024

I faced the similar issue.
Mac + Sourcetree + husky stuck with using an old node version installed via nvm ignoring the new nvm default node version
Once I deleted that old node version I get npx not found error (because PATH contained the path to an old node version still)

The problem was that another program had added PATH changes to ~/.zshrc (.bashrc if you use it)
The solution was to move the nvm setup to the bottom of .zshrc (.bashrc)

Problem

# NVM initialisation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"

Solution

# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"

# NVM initialisation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

stackoverflow

Checking ~/.huskyrc config helped as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests