husky: sh: husky: command not found

Context

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?

Thank you!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 17 (2 by maintainers)

Most upvoted comments

@ctsstc That didn’t work for me.

The following worked (and is cross platform too):

"prepare": "node -e \"if (process.env.NODE_ENV !== 'production'){process.exit(1)} \" || husky install",

Everything seems alright on local machine, but when I deploy on Heroku, I get this:

...
remote: -----> Installing dependencies
remote:        Installing node modules
remote:
remote:        > drcm-bot@0.1.0 prepare /tmp/build_b8e6fb4d
remote:        > husky install
remote:
remote:        sh: 1: husky: not found
remote:        npm ERR! code ELIFECYCLE
remote:        npm ERR! syscall spawn
remote:        npm ERR! file sh
remote:        npm ERR! errno ENOENT
remote:        npm ERR! drcm-bot@0.1.0 prepare: `husky install`
remote:        npm ERR! spawn ENOENT
...

The problem was that husky, being installed in devDependencies, gets stripped away if you set NPM_CONFIG_PRODUCTION=true.

If you use command like npm ci --omit=dev you can access those property via $npm_config_omit. According to value of $npm_config_omit you can decide to run husky install command or not.

{
  "postinstall": "if [ \"$npm_config_omit\" = \"dev\" ]; then echo \"Skip husky installation\"; else husky install; fi"
}

I seem to have no problems on a new project on heroku with:

  • node: v16.13.2
  • npm: 8.1.2
  • husky: 7.0.4

On the other project when I originally commented here I ended up doing:

"prepare": "if [[ $NODE_ENVIRONMENT != \"production\" ]]; then husky install; fi"

Note: It was running in a docker container up on AWS.

Another way:

  "scripts": {
    "prepare": "husky install || true",

I ran into this error. Turns out something on my laptop was setting NODE_ENV="production", and husky wasn’t being installed because it’s a dev dependency. Setting NODE_ENV to development fixed the problem.

I ran into the same error on Vercel, and found this issue while Googling for the error «sh: husky: command not found». In case anyone comes here from the same Vercel context, although not specifically linked to the issue above, this might help:

My issue was that I am using NPM workspaces within a mono repo, and I set the root directory of the Vercel project to docs/ subfolder (one of the workspaces) — which has its own docs/package.json with no Husky in its dev dependencies. Nevertheless, the "prepare": "husky" script was being invoked from the parent package.json during install.

Resetting the root directory of the Vercel project to the root of the monorepo solved the issue: npm install running from the root of the mono repo has a package.json with husky in its dev dependencies and its "prepare": "husky" script runs smoothly.

Vercel › Project XYZ › Settings › Root Directory