Skip to content

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

Closed
@lamyergeier

Description

@lamyergeier

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.

Activity

rhefner

rhefner commented on Jan 31, 2019

@rhefner

Seeing the same thing. Executing a commit manually, husky gets involved and works as intended. Executing a commit via VS Code, it fails:

> git commit --quiet --allow-empty-message --file -
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
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
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
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
> git status -z -u
> git symbolic-ref --short HEAD
...

I tried pre-commit before husky, which worked. But, figured that husky supports so much more and is popular, so I would try it instead.

rhefner

rhefner commented on Jan 31, 2019

@rhefner

Ah, just saw #282 as well.

rhefner

rhefner commented on Jan 31, 2019

@rhefner

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!

brandon-pereira

brandon-pereira commented on May 23, 2019

@brandon-pereira

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

phifa

phifa commented on Jun 27, 2019

@phifa

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

typicode

typicode commented on Jun 27, 2019

@typicode
Owner
phifa

phifa commented on Jun 27, 2019

@phifa

image
It looks like it works... I am getting this message, when opening the log I see the eslint errors from husky. Is this how it's supposed to work?

jcollum-autodesk

jcollum-autodesk commented on Sep 18, 2019

@jcollum-autodesk

@phifa sounds right to me, eslint errors means you ... have eslint errors

gabrielmicko

gabrielmicko commented on Sep 23, 2019

@gabrielmicko

What worked for me is creating ~/.huskyrc and defining node path.

rfgamaral

rfgamaral commented on Oct 25, 2019

@rfgamaral

What worked for me is creating ~/.huskyrc and defining node path.

How exactly did you do that?

typicode

typicode commented on Jan 7, 2020

@typicode
Owner

Closing to clean up. Feel free to create another issue if you still have problems.

thasmo

thasmo commented on Mar 27, 2020

@thasmo

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.

icetbr

icetbr commented on May 7, 2020

@icetbr

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

Zeal29

Zeal29 commented on Sep 29, 2020

@Zeal29

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
Thlnking

Thlnking commented on Mar 11, 2022

@Thlnking

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

Repository owner locked as resolved and limited conversation to collaborators on Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rfgamaral@thasmo@icetbr@phifa@gabrielmicko

        Issue actions

          Can't find Node.js installed with NVM, when used in VS Code · Issue #438 · typicode/husky