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"
},
Create a hook To add a command to a hook or create a new one, use husky add <file> [cmd] (don't forget to run husky install before). If npm test command fails, your commit will be automatically aborted.
To install the hook, you can either create a symlink to it in . git/hooks , or you can simply copy and paste it into the . git/hooks directory whenever the hook is updated. As an alternative, Git also provides a Template Directory mechanism that makes it easier to install hooks automatically.
If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook.
Husky is a dotnet tool that allows you to run arbitrary actions on specific git hooks. These could be to enforce the same sort of things that you would do centrally, but here they would run before committing code into git. For example, you could ensure your code built and passed all unit tests.
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]
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
.
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 🙂
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With