'sh: husky: command not found

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?



Solution 1:[1]

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

Solution 2:[2]

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"

Solution 3:[3]

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.

Solution 4:[4]

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"

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chandelier Axel
Solution 2 Rajush
Solution 3 David Ferreira
Solution 4 Hossein Mousavi
Solution 5 lukejkw