'add husky pre-commit hook with npm@6 on windows
Environment:
git version 2.25.1.windows.1
Npm 6.14.11
husky 7.0.4
windows 10
How to correctly setup husky & pre-commit hook? In my case I just want to add npm test script to run, so with every commit tests are run.
Solution 1:[1]
Although most of things are covered in docs (if not all), I will share steps which I took in order to make this work because I have put several hours effort before finally made it.
npm i husky -D- add new script to
package.json-"prepare": "husky install" - execute
npm run prepare-> .husky folder is created in project root directory
- add new script in
package.jsonfile (read more in additional at the bottom):
- "husky": "node ./node_modules/husky/lib/bin.js"
- run
npm run husky add ./husky/pre-commit "npm test"->pre-commithook is created
ADDITIONAL explanation to step 4:
npx husky add .husky/pre-commit "npm test" - this is command stated in docs and many stackoverflow questions/github issues or blogs, but this doesn't work by default on Windows & npm@6 (at least not for me).
Output after this command is like this, it is just showing how to use command, no pre-commit file is created inside .husky folder:
It is documented in husky docs how this can be approached differently, and that is what I tried to do in step 4.
For Windows users, if you see the help message when running npx husky add ..., try node node_modules/.bin/husky add ... instead. This isn't an issue with husky code and is fixed in recent versions of npm 8.
- And that is all. No additional actions, nor I didn't have to unset git config core.hooksPath.
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 |




