husky: Bug commit from vscode

After upgrade to husky v5, git commit from vscode always fail (via terminal is ok).

image

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
.husky/pre-commit: line 4: npx: command not found
husky - pre-commit hook exited with code 127 (error)

npm version: 7.5.1 node version: 15.8.0

Hooks

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

npx lint-staged

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 62
  • Comments: 50 (1 by maintainers)

Most upvoted comments

restart vscode solved my problem…

I use VS Code and Husky v6.

I ran echo $PATH from the root of my project. Then I copied the output to the pre-commit file in the .husky directory.

My pre-commit file looks like this:

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

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
npx lint-staged

Solved the issue for me.

This is the ~/.huskyrc that worked for me on my MacBookPro on BigSur 11.6

# First load nvm and provide access to the nvm command.
export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"

# Use the nvm ls command to detect the version being used.
export NVM_DIR="$HOME/.nvm"
a=$(nvm ls | grep 'node')
b=${a#*(-> }
v=${b%%[)| ]*}

# Export the current version in your path for husky to find.
export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"

I hope this helps someone. I definitely think this needs to be done in your local project directory moving forward.

Hi @typicode There are too many such problems. May I suggest that this step should be implemented by the package itself(husky.sh). In this way, most users do not need to set up the .huskyrc file. Although more work needs to be done, I think it is worth it. I can provide the code I have used(pre-commit)

#!/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"
yarn lint-staged

Or change “~/.huskyrc” to “.huskyrc”. It is convenient for a third party to help modify it. For the great library, cheers!

I use VS Code and husky 7. Yesterday everything was working, today I tried to commit and got the same error .husky/pre-commit: line 4: npm: command not found

Having the same issue, but the suggestion by typicode doesn’t work:

# .huskyrc
# See https://typicode.github.io/husky/#/?id=command-not-found
# 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"

I guess the issue can be resolved by updating the $PATH used by VSCode’s Git process, but not sure where to find that.

My solution was to commit via git bash on windows, not via gitHub desktop. git commit -m "First commit"

FYI for those using brew to determine paths, apparently that can cause issues, too.

This definitely seems like a bug in VSCode. In the meantime, you’ll probably need to just reference the exact path needed for anything relying on brew --prefix whatever.

For example, I use asdf for versioning npm. asdf has a setup script that gets all the shims set properly in your path, and that script is usually loaded by running . "$(brew --prefix asdf)/asdf.sh" in your .zshrc file. So in my case, I needed to just hardcode the path returned from brew and put the resulting command in my ~/.huskyrc file:

. /opt/homebrew/opt/asdf/asdf.sh

Hi @RigottiG, could you try https://typicode.github.io/husky/#/?id=command-not-found

Is there any way I can solve this within the project folder ? we have a large group of developers (and it’s growing), I cannot tell everyone to create a husky config file on their computer to solve the issue.

This problem may trouble most of developers who use vscode’s git to commit, cause it might not resolve the correct path, at least in the version 1.58.0

Just dropping this here incase someone was puzzled by why it just “suddenly” happened in my case it was because: I usually open vscode via the terminal (which contains my env vars) but after restarting my mac, vscode opened automatically (which did not start up with my env vars - which are exported in my shell rc)

I installed node using NVM. So simply adding the code mentioned in the docs here for nvm in the code example worked: https://typicode.github.io/husky/#/?id=command-not-found

In your terminal:

code ~/.huskyrc

Then paste the code:

# 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"

Save the file. And then source it. In the terminal:

source ~/.huskyrc

Performing the commit should work ok.

It happened to me when I upgraded my vscode from 1.59 to 1.60 today. My project suddenly not work.

It worked well yesterday.

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

In the meantime, I was able to resolve my issue by setting the right PATH variables via source ~/.profile in the pre-commit file

On my macOS Big Sur, what “solved” for me was unchecking the Terminal > Integrated: Inherit Env setting in VSCode (it’s located in the User area).

In my case, I installed node using the standard .pkg installer and it made my node, npm and npx available on /usr/local/bin, but for “some reason” when I did echo $PATH from within the pre-commit husky file, it didn’t show /usr/local/bin, so that’s why I was having this same npx: command not found problem.

After I unchecked this setting and restarted VSCode, I was able to commit via VSCode GUI again.

@RigottiG I also had the issue with VSCode. I’m using nvm, and creating an ~/.huskyrc file as the Typicode recommended fixed it.

This is the ~/.huskyrc that worked for me on my MacBookPro on BigSur 11.6

# First load nvm and provide access to the nvm command.
export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"

# Use the nvm ls command to detect the version being used.
export NVM_DIR="$HOME/.nvm"
a=$(nvm ls | grep 'node')
b=${a#*(-> }
v=${b%%[)| ]*}

# Export the current version in your path for husky to find.
export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"

I hope this helps someone. I definitely think this needs to be done in your local project directory moving forward.

Thanks! That worked, agree that it shouldn’t be done globally.

I just tried update and restart on VScode, and it worked!

For anyone facing the issue from WSL2, my PATH (check with echo $PATH in terminal) included paths from Windows OS and for me it worked to enable appendWindowsPath=false at /etc/wsl.conf, so my wsl config looks like this now:

[interop]
appendWindowsPath=false

After that wsl has to be restarted, e.g. from PowerShell run wsl --shutdown and open vs code with remote repository again. Hope that helps someone

for fish and nvm.fish

# Export the current version in your path for husky to find.
export PATH="$HOME/.local/share/nvm/v16.20.0/bin:$PATH"

solved the issue for me.

I’ve got the latest version of Codium:

rildev@workstation:~/Code/mpn/client$ codium --version
1.67.2
c3511e6c69bb39013c4a4b7b9566ec1ca73fc4d5
x64

In my case, Husky doesn’t work with the IDE’s source control’s tab. It actually gives me an error pop-up. But it works fine with the command line.

An alternative I found is Lefthook. It is easy to setup, works out of the box BUT is not triggered by the Codium’s source control tools.

So it doesn’t block my normal workflow, but to use the pre-commit hooks, I have to switch to my terminal.

adding 2 cents: it happened with me when I switched default shell from bash to zsh

I downgraded VS Code to the previous version and it fixed the problem.

Just dropping this here incase someone was puzzled by why it just “suddenly” happened in my case it was because: I usually open vscode via the terminal (which contains my env vars) but after restarting my mac, vscode opened automatically (which did not start up with my env vars - which are exported in my shell rc)

@TomerAtarScopio Thank you!!!

restart vscode solved my problem…

that worked for me, but why?

Is there any fix for this that does not require that developers make local configuration or that their environments are so similar that a check-in path is true for all of them? I work on a team with many developers in the same repository and as it stands now we are looking at removing husky and lint-staged as there doesn’t seem to be a team-wide solution.

it’s VSCode Issue, you can upgrade VSCode version to latest

I find two temporary solution like that

git config --unset core.hooksPath  

or

git commit -m "message" --no-verify 

But these are not permanent solutions. if you change pre-commit file as suggested, you can commit your changes without any trouble.

#!/bin/sh
# ~/.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"

I messed with my nvm list and forgot to switch to my work node version. I was sitting on version without installed yarn. So the resolve was to install yarn globally npm i -g yarn or to switch to my work node version where the yarn was already installed.

On Windows, I deleted C:/Users/{username}/AppData/Roaming/npm and C:/Users/{username}/AppData/Roaming/npm-cache folders and reinstalled global npm modules like Yarn, and the problem fixed.

Hi @RigottiG, could you try https://typicode.github.io/husky/#/?id=command-not-found

Is there any way I can solve this within the project folder ? we have a large group of developers (and it’s growing), I cannot tell everyone to create a husky config file on their computer to solve the issue. This problem may trouble most of developers who use vscode’s git to commit, cause it might not resolve the correct path, at least in the version 1.58.0

you found any solution?

same here. Did you find a way to fix this?

The same issue with Webstorm and npm: .husky/pre-push: 4: npm: not found

I have tried to run npm run lint on pre-commit. Everything is fine when I use a terminal for commit but not the Webstorm commit dialog.