Husky v5 doesn't create Git hooks

10.9k views Asked by At

I've installed Husky v5 in my application and I would like to run the lint-staged command upon commiting.

I've followed the Getting Started docs but no .git/hooks/pre-commit file has been created in my git configuration files.

So, when I commit, the hook is not ran and the commit passes straight away without being checked by lint-staged.

I tried running yarn add -D husky@next or npm i -D husky@next. I also tried removing node_modules and npm rebuild.

.husky/pre-commit

#!/bin/sh
[ -z "$CI" ] && exit 0

. "$(dirname $0)/_/husky.sh"

lint-staged

package.json

"scripts": {
  "postinstall": "husky install"
},
3

There are 3 answers

1
Константин Подмаско On BEST ANSWER

husky v5 does not generates hooks (can not say why)
so I downgraded to 4.3.8 and removed .git/hooks(not necessary):

rm -rf .git/hooks
yarn add -D [email protected]
0
BigglesZX On

A little late, but I had this problem today too. After much searching I found this issue which describes installation problems involving Yarn. In my case yarn was not properly running the post-install script from husky and as advised on that thread I found changing my postinstall line in package.json to this solved my problem:

{
    "postinstall": "node ./node_modules/husky/lib/installer/bin install"
}

I was running and re-running installation several times from various locations while finalising my setup. I found this list of instructions helpful in making sure I was resetting my git config to a consistent state each time, in particular the line mentioning hooksPath.

0
centner On

You'll need to add yarn before lint-staged in your .husky/pre-commit file:

#!/bin/sh
[ -z "$CI" ] && exit 0

. "$(dirname $0)/_/husky.sh"

yarn lint-staged

That's because of:

If you were calling directly locally installed binaries, you need to run them via your package manager

More info you could find here and here. Hope that helps