Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Husky v5 doesn't create Git hooks

Tags:

git

husky

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"
},
like image 799
Théo Lavaux Avatar asked Nov 30 '20 12:11

Théo Lavaux


People also ask

How do you put a hook on a Husky?

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.

How do I enable a git hook?

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.

How do I enable pre-commit hook?

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.

What is husky git hooks?

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.


3 Answers

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]
like image 131
Константин Подмаско Avatar answered Oct 19 '22 04:10

Константин Подмаско


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.

like image 44
BigglesZX Avatar answered Oct 19 '22 05:10

BigglesZX


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 🙂

like image 36
centner Avatar answered Oct 19 '22 03:10

centner