sh: husky: command not found

Viewed 46884

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?

8 Answers

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"

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.

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"

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

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

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

Faced this issue in Github Desktop.

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

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

  "scripts": {
    "prepare": "node_modules/.bin/husky-run install"
  },
Related