husky: Bug commit from vscode
After upgrade to husky v5, git commit from vscode always fail (via terminal is ok).
> 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)
restart vscode solved my problem…
Hi @RigottiG, could you try https://typicode.github.io/husky/#/?id=command-not-found
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:
Solved the issue for me.
This is the
~/.huskyrc
that worked for me on my MacBookPro on BigSur 11.6I 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)
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:
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 versioningnpm
.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 frombrew
and put the resulting command in my~/.huskyrc
file: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:
Then paste the code:
Save the file. And then source it. In the terminal:
Performing the commit should work ok.
It happened to me when I upgraded my vscode from
1.59
to1.60
today. My project suddenly not work.It worked well yesterday.
In the meantime, I was able to resolve my issue by setting the right PATH variables via
source ~/.profile
in the pre-commit fileOn my macOS Big Sur, what “solved” for me was unchecking the
Terminal > Integrated: Inherit Env
setting in VSCode (it’s located in theUser
area).In my case, I installed
node
using the standard.pkg
installer and it made mynode
,npm
andnpx
available on/usr/local/bin
, but for “some reason” when I didecho $PATH
from within thepre-commit
husky file, it didn’t show/usr/local/bin
, so that’s why I was having this samenpx: 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.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 enableappendWindowsPath=false
at/etc/wsl.conf
, so my wsl config looks like this now: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 someonenice article with explanation and fix
https://ntsim.uk/posts/running-git-hooks-with-environment-variables-in-intellij
for
fish
andnvm.fish
solved the issue for me.
I’ve got the latest version of Codium:
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
tozsh
I downgraded VS Code to the previous version and it fixed the problem.
@TomerAtarScopio Thank you!!!
that worked for me, but why?
it’s VSCode Issue, you can upgrade VSCode version to latest
I find two temporary solution like that
or
But these are not permanent solutions. if you change pre-commit file as suggested, you can commit your changes without any trouble.
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
andC:/Users/{username}/AppData/Roaming/npm-cache
folders and reinstalled global npm modules like Yarn, and the problem fixed.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.