sh: husky: command not found
Asked Answered
M

11

89

I've setup a node project with husky but when my collegue tries to run npm install on his Mac he gets the following error :

[email protected] prepare
husky install

sh: husky: command not found
npm ERR! code 127
npm ERR! path /Users/X/Desktop/Workspace/project
npm ERR! command failed
npm ERR! command sh -c husky install

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/X/.npm/_logs/2021-04-12T13_07_25_842Z-debug.log

These are the relevant package.json parts:

{
    "scripts": {
        "prepare": "husky install"
    },
    "devDependencies": {
        "husky": "^5.2.0",
    }
}

I thought this would be enough for husky to be installed when running npm install, but it's not. What am I missing?

Megawatt answered 12/4, 2021 at 18:43 Comment(1)
This error is also thrown by npm ci if the NODE_ENV is set to "production" pre-installFullblooded
H
11

I've been able to solve the problem by upgrading to latest Husky version (7.0.1, from 5.2.0).

Git was also helpful, and told me that the files weren't executables. (Git V 2.24.1)

So I give them executable rights :

chmod +x PATH_TO_HUSKY_FILE

You'll need to execute this command for every hooks

Habitue answered 19/7, 2021 at 20:5 Comment(0)
P
183

If you are using nvm, you might want to create a file called .huskyrc in your home directory and add the following lines of code to it:

~/.huskyrc

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Polite answered 17/5, 2022 at 18:47 Comment(5)
Also, don't forget to install husky on the devDependencies.Solar
Does this apply to Windows? If so, where is this home file exactly?Moonseed
I go the following error message when trying to commit from IntelliJ (worked fine directly from terminal): .husky/pre-commit: 4: npm: not found. Your suggestion fixed my issue. Thanks!Bobker
Commits were working fine for me using CLI, but Tower.app was complaining with an error. After I added this Tower was able to commit.Fransen
Please explain how this answers the original question, because as far as I can tell this is completely unrelated.Despite
C
31

Faced this issue in Github Desktop.

solved it by quit Github Desktop and re-open it.

Cowper answered 25/7, 2022 at 15:14 Comment(2)
So simple. Love it. Thank you 🙏Consistency
This fixed it on VSCode as well.Andrewandrewes
B
29

I was struggling with the same exact problem for hours. Finally, I could install dependencies and start working on my project by doing this:

  1. Temporarily remove the "prepare": "husky install" script from the package.json file.
  2. Run npm i (npm install). Dependencies installed successfuly.
  3. Add again the "prepare" script that you removed in step 1.
  4. Run again npm i to install the husky git hooks, so husky can do its job from now on.
Bannockburn answered 20/10, 2021 at 19:2 Comment(3)
Thank you so much. Any idea the reasoning behind why we have to remove prepare and why husky isn't built in a way to avoid this?Rematch
Incredible, but this works.Hawkeyed
If using VSCODE you might need to close and open it again as well (in macOS at least).Crist
T
22

It worked in my terminal but not in VSCode version control. So had to force quite the vscode app and restarting it worked.

Thoughtout answered 14/6, 2022 at 13:0 Comment(4)
Yep, this fixed it in vscode. Why...like most broken things, it doesn't pay to figure it out.Imagination
It fixed in my case, MacOS Ventura M2, zsh (oh-my-zsh) with fnm and starship.Coppage
+1 since it worked in my Ubuntu terminal even though it's not working in VSC. Quitting and opening VSC didn't help. But at least I know that lint-staged and husky can work on my system if I don't use VSC.Nichani
Force quitting VS Code and restarting it fixed it for me too... Weird... Intel Mac OS Catalina, zsh (oh-my-zsh)Triumvir
H
11

I've been able to solve the problem by upgrading to latest Husky version (7.0.1, from 5.2.0).

Git was also helpful, and told me that the files weren't executables. (Git V 2.24.1)

So I give them executable rights :

chmod +x PATH_TO_HUSKY_FILE

You'll need to execute this command for every hooks

Habitue answered 19/7, 2021 at 20:5 Comment(0)
B
10

I believe it could be version specific issue. Install version 6, npm i [email protected] --save-dev, and it should work as the husky doc says.

Apparently, when I did npm i husky --save-dev, it was installing "husky": "^0.8.1" for me for some strange reason, giving me the exact same error: sh: husky: command not found.

Method 1:

Update manually, in your package.json:

{
    "scripts": {
        "prepare": "husky install",
        "create-hook": "husky add .husky/pre-commit \"npm test\"",
    }
}

Then, run npm run prepare && npm run create-hook.

It should create .husky directory with .pre-commit file in it.

Method 2:

npx husky install

npm set-script prepare "husky install"

npx husky add .husky/pre-commit "npm test"

Birdiebirdlike answered 5/5, 2021 at 19:36 Comment(0)
E
4

I was able to fix this by providing an explicit location for husky

  "scripts": {
    "prepare": "node_modules/.bin/husky-run install"
  },
Emelinaemeline answered 29/8, 2022 at 5:24 Comment(0)
W
2

What .huskyrc needs to do is:

  1. Check if there is a .nvmrc file in the working directory
  2. Run the nvm.sh script
  3. Run nvm use, so all node related executables (eg: yarn) will be ready to use

Thus, here is a version of .huskyrc that fulfil those requisites:

export NVM_DIR="$HOME/.nvm"

if [ -f .nvmrc ] && [ -s "$NVM_DIR/nvm.sh" ];
then
  . "$NVM_DIR/nvm.sh"
  nvm use
fi;
Weathering answered 25/4, 2023 at 16:9 Comment(1)
Instead of adding those to my ~/.huskyrc, I added it directly to the project/.husky/pre-commit & project/.husky/pre-push since my team is also using nvm.Adduce
S
2

In case you're facing this issue while installing the production dependencies you can use this solution

npm set-script prepare '' && npm install --omit=dev
Staples answered 5/5, 2023 at 11:42 Comment(2)
This is good for production because it bypasses the unnecessary husky setupAbstruse
npm set-script prepare doesn't work in newer versions of npm, it should be replaced witih npm pkg delete scripts.prepareTeocalli
K
2

For peoples in Windows using WebStorm, Gitkraken etc... if you encounter this error, try to setup C:\Program Files\Git\bin for Path in Environment Variables System. It may help you. Do not forget to restart IDE/terminal after it. enter image description here

Keelson answered 18/8, 2023 at 15:29 Comment(0)
M
0

Using Lerna

When I upgraded husky from version 4 to 8 there was information todo first pre commit manually. For this purpose pre-commit bash script was generated in .husky directory.

What I had todo was simply run the command included in this file:

lerna run precommit --concurrency 2 --stream
Manion answered 29/11, 2022 at 23:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.