VSCODE & GitHub Desktop pre-commit hook: npx: command not found
Asked Answered
E

11

63

I am starting a new repo, thinking I should use the most recent Huksy v6 which is installed from LintStaged using their setup guide:

npx mrm lint-staged

// package.json updated with:
"husky": ">=6",
"lint-staged": ">=10",

This adds necessary packages and adds the husky files including the precommit files:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged

When i run my commit in the terminal it works fine. However, if I try to run my commit in GitHub Desktop or VSCode (which I know some teammates do), it results in an error for both:

npx: command not found. husky - pre-commit hook exited with code 127 (error)

I have npx installed:

npx -v
// 6.14.10

If I try to install in globall, such as described in other StackOverflow suggestions, it returns a warning about existing location (with & with out sudo):

ERR! EEXIST: file already exists, symlink '../lib/node_modules/npx/index.js' -> '/Users/plucks/.nvm/versions/node/v14.15.4/bin/npx' npm ERR! File exists: /Users/plucks/.nvm/versions/node/v14.15.4/bin/npx npm ERR! Remove the existing file and try again, or run npm npm ERR! with --force to overwrite files recklessly.

Is there anything I can do so the programs like VSCode & GitHub Desktop can run?

Eaglet answered 15/4, 2021 at 20:52 Comment(0)
E
17

I had to combine the answers of Cathal and Misol.

I did not want to edit the .husky/pre-commit like Cathal for two reasons:

  1. I would need to that for every project I use husky in
  2. It would actually break husky for my fellow developers on Windows

So I added a global ~/.huskyrc file like Misol did with the following contents:

export NVM_DIR="$HOME/.nvm/nvm.sh"
. "$(dirname $NVM_DIR)/nvm.sh"

export NVM_DIR="$HOME/.nvm"
a=$(nvm ls | grep 'node')
b=${a#*(-> }
v=${b%%[)| ]*}

export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"
Evadne answered 16/3, 2022 at 9:16 Comment(4)
Works like a charm, very nice.Xerophthalmia
this is the only solution which worked for both VSCode & Github Desktop for meEaglet
where you added this file which directoryDivagate
As mentioned in my answer it's in the home folder. That what tilde (~) means. You can read more about that here: gnu.org/software/bash/manual/html_node/Tilde-Expansion.htmlEvadne
R
99

I've got the solution from here. Hope you can find it as well!

Here it is, for clarity:

  • add a file ~/.huskyrc if you don't have one already
  • make sure it includes the following:
# ~/.huskyrc
# 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"
Rockandroll answered 11/5, 2021 at 7:44 Comment(1)
Prefer to add this into .husky/pre-commit because you can share it via gitLandel
T
50

Before adding any modifications to your project try restarting your IDE as mentioned in this issue

Tomfool answered 7/9, 2022 at 12:27 Comment(4)
I highly recommend other people try this first. This worked for me and saved a lot of hassle of not having to write a bunch of bash scripting or custom configuring.Phallus
Yes restarting fixed it for me as well, it was working fine for me with VS Code and started breaking for me all of a sudden, I tried reloading the window that didn't work out but if you restart the IDE that fixes it.Cholinesterase
This response should be on the top! Thanks sir, I was about to go crazy on this issue!Nebulize
I had this issue with GitHub Desktop on macOS after updating xcode, and restarting it fixed it for me as well.Crossbench
S
21

As per this suggestion, adding the following to your pre-commit file should fix it:

export NVM_DIR="$HOME/.nvm/nvm.sh"
. "$(dirname $NVM_DIR)/nvm.sh"

export NVM_DIR="$HOME/.nvm"
a=$(nvm ls | grep 'node')
b=${a#*(-> }
v=${b%%[)| ]*}

export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"

So the full file would look like this:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

export NVM_DIR="$HOME/.nvm/nvm.sh"
. "$(dirname $NVM_DIR)/nvm.sh"

export NVM_DIR="$HOME/.nvm"
a=$(nvm ls | grep 'node')
b=${a#*(-> }
v=${b%%[)| ]*}

export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"

npm run test
Soundless answered 6/8, 2021 at 21:38 Comment(2)
Thanks for this! I'm running Ubuntu and added that first block of text to ~/.huskyrc and fixed the issue when doing a commit from GitHub Desktop.Draught
what if you dont use nvm?Hotchpot
F
17

For husky>=6: update your .husky/pre-commit file to contain this:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

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

npx lint-staged

This will find and expose the current node path and therefore npx path that you are using, which has been configured with Node Version Manager nvm

Feeze answered 10/2, 2022 at 20:19 Comment(0)
E
17

I had to combine the answers of Cathal and Misol.

I did not want to edit the .husky/pre-commit like Cathal for two reasons:

  1. I would need to that for every project I use husky in
  2. It would actually break husky for my fellow developers on Windows

So I added a global ~/.huskyrc file like Misol did with the following contents:

export NVM_DIR="$HOME/.nvm/nvm.sh"
. "$(dirname $NVM_DIR)/nvm.sh"

export NVM_DIR="$HOME/.nvm"
a=$(nvm ls | grep 'node')
b=${a#*(-> }
v=${b%%[)| ]*}

export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"
Evadne answered 16/3, 2022 at 9:16 Comment(4)
Works like a charm, very nice.Xerophthalmia
this is the only solution which worked for both VSCode & Github Desktop for meEaglet
where you added this file which directoryDivagate
As mentioned in my answer it's in the home folder. That what tilde (~) means. You can read more about that here: gnu.org/software/bash/manual/html_node/Tilde-Expansion.htmlEvadne
A
3

For those using fnm instead of nvm, adding the following to ~/.huskyrc worked for me:

eval "$(fnm env)"
Alvira answered 24/8, 2022 at 14:22 Comment(0)
K
2

If you are working on a team with other people who may have installed nvm or node in slightly different fashion than you have, I would not recommend adding any export statements or edits to the $PATH in your .husky/precommit or ~/.huskyrc files.

If you want your VSCode to have proper access to the $PATH you expect from terminal you should always launch VSCode from terminal in the folder you are working from.

For example, in a terminal window:

~/_git/my_project: code .

Will launch VSCode with my_project open in a new window (it should remember your tabs and window state from the last time you worked on your project).

VSCode will now use the $PATH your terminal uses from your ~/.zshrc or ~/.bashrc, etc.

With this setup my .husky/precommit looks like this:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged

and my my .lintstagedrc.json looks like this:

{
  "*.{js,jsx,ts,tsx}": [
    "eslint --fix --debug --max-warnings=-1",
    "npm run lint:prettier-fix"
  ],
  "*.{css,less,sass,scss}": ["npm run lint:prettier-fix"],
  "*.{g?(raph)ql,json,html,md,y?(a)ml}": ["npm run lint:prettier-fix"]
}
Keddah answered 24/3, 2022 at 19:9 Comment(1)
This answer is unhelpful. People aren't going to only ever launch VSCode and Github Desktop via CLI from a project folder.Harlotry
L
1

The other suggestions are fine until you have only one node version if you have two node versions and in one of them you don't have the yarn, you will face this issue, so do a simple change

# ~/.huskyrc
# 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"

# This take the current active node version you want to verify the hook
export NVM_DIR="$HOME/.nvm"
a=$(nvm current)

export PATH="$NVM_DIR/versions/node/$a/bin:$PATH"
Legnica answered 12/3, 2023 at 3:54 Comment(0)
G
1

I've found adding this worked to get past the error

unset ELECTRON_RUN_AS_NODE

My pre-push looks like this

unset ELECTRON_RUN_AS_NODE

npm run cy

However, this is still not working as I'm getting a git "failed to push some refs to remote" error. I confirmed that there are no remote changes so something else was messed up by doing this. I may just have to give up on the husky pre-push for now.

Gebelein answered 26/4, 2024 at 1:0 Comment(0)
T
0

Open VSCode settings and set the Inherit Env setting (Terminal > Integrated: Inherit Env) to false:

"terminal.integrated.inheritEnv": false

This setting enables or disables whether new shells should inherit their environment from VS Code.

Tool answered 27/7, 2022 at 7:36 Comment(0)
D
0

I change code as the top answer says. But it doesn't works at first, and then reopen VScode and it works.

In the terminal, I input these commands:

  1. copy this command in terminal and press enter.

vi ~/.huskyrc

  1. copy this command in terminal.

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

  1. input :wq to quit edit state

  2. reopen VScode.

Disparagement answered 18/10, 2022 at 10:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.