husky: Can't find Node.js installed with NVM, when used in VS Code

Husky stopped working in VS Code (latest) after I installed node.js (LTS) with node version manager on Ubuntu 18.4. But it works perfectly in the terminal.

$ type node
node is /home/nikhil/.nvm/versions/node/v10.15.0/bin/node

It is unable to find node.js and shows the following error in the Git Output inside VS Code integrated terminal:

Can't find node in PATH, trying to find a node binary on your system
Couldn't find the Node.js binary. Ensure you have Node.js installed. Open an issue on https://github.com/sindresorhus/run-node

What should I do?


NOTE: In VS Code, Husky was working fine when nodejs was installed with sudo in priviledged location.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Looks like adding a ~/.huskyrc file with:

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

Stops VS Code from committing and allows husky to continue, but run-node still spits out one error message and VS Code will use that error as it’s display: “Git: Can’t find node in PATH, trying to find a node binary on your system” … But, gives you a chance to open the Git log and you can see that Husky did indeed run:

> git commit --quiet --allow-empty-message --file -
> git show :server/routes/middleware.ts
Can't find node in PATH, trying to find a node binary on your system
husky > pre-commit (node v11.2.0)

> project@0.5.0 prettier:quick:fix /Users/hef/work/project
> pretty-quick --staged --pattern "$npm_package_config_prettier_regex_all"

🔍  Finding changed files since git revision 03ce41a.
🎯  Found 1 changed file.
✅  Everything is awesome!
Stashing changes... [started]
Stashing changes... [skipped]
→ No partially staged files found...
Running linters... [started]
Running tasks for *.{ts,tsx} [started]
tslint [started]
tslint [failed]
→ 
Running tasks for *.{ts,tsx} [failed]
→ 
Running linters... [failed]



✖ tslint found some errors. Please fix them and try committing again.

ERROR: server/routes/middleware.ts[81, 47]: Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.


Warning: The 'no-use-before-declare' rule requires type information.
husky > pre-commit hook failed (add --no-verify to bypass)
> git config --get-all user.name
> git config --get-all user.email

…Least it still works. IF someone has a better solution, would love to hear it. This is just a hack!

The only proper solution I found was to create a ~/.huskyrc file, as mentioned in the husky docs:

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

I added nvm use so it will load the actual node version defined in the project’s .nvmrc file.

Adding the ~/.huskyrc worked for me. Thanks @rhefner

Is there anywhere a a guide how to make husky work when used in nvm / vscode environment?

nvm + husky + VS Code source control = is not working for me but I fixed it.

What worked of me is (I am not an expert something might optional you can see, but they make my project working with VS Code source control)

  1. i install husky + run-node
  2. run command on terminal nvm use --delete-prefix v14.12.0 <- use then node version you want to use
  3. run command on terminal nvm use v14.12.0
  4. run command on terminal open ~/.huskyrc if file dont exit create it with touch ~/.huskyrc
  5. the content of my ~/.huskyrc file is
PATH="/usr/local/bin:$PATH"

#!/usr/bin/env bash

source ~/.bash_profile

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


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

6.(optional) add these 2 lines in you ~/.bash_profile if still not working because I added and too lazy to remove them

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

As an alternate solution, this at least gives a proper error message on hook fail

sudo ln -s -f `npm config get prefix`/bin/node /usr/bin/node