68

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 :

noa-be@1.0.0 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?

Hossein Mousavi
  • 3,118
  • 3
  • 16
  • 35
GaelF
  • 877
  • 1
  • 6
  • 8

12 Answers12

155

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"
Hossein Mousavi
  • 3,118
  • 3
  • 16
  • 35
23

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.
David Ferreira
  • 1,233
  • 17
  • 24
  • 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? – Evan Erickson May 30 '23 at 14:58
16

This error is also thrown by npm ci if the NODE_ENV is set to "production" pre-install

lukejkw
  • 1,054
  • 13
  • 19
  • 2
    Good point. I'd like to see this as a comment near the question as it's not a real answer to the issue – dance2die May 28 '22 at 14:47
14

Faced this issue in Github Desktop.

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

Itay Tur
  • 683
  • 1
  • 15
  • 40
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

Chandelier Axel
  • 237
  • 2
  • 6
9

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

Norfeldt
  • 8,272
  • 23
  • 96
  • 152
8

I believe it could be version specific issue. Install version 6, npm i husky@6.0.0 --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"

Rajush
  • 635
  • 6
  • 10
2

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

  "scripts": {
    "prepare": "node_modules/.bin/husky-run install"
  },
dtruong0
  • 41
  • 5
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
Konrad Grzyb
  • 1,561
  • 16
  • 12
0

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;
0

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
0

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

Alexander Gvozd
  • 121
  • 1
  • 6