husky: git commit hooks never get created when husky v5 is installed

Installing husky 5 with npm i husky doesn’t create the commit hooks when used with git version 2.20.1, node v14.15.4, and npm v6.14.10 on mac in a new project. I confirmed this by opening the .git folder for my project and checking the hooks folder, and it was unchanged after installation. I think I was having the same issue as several other people in this issue: https://github.com/typicode/husky/issues/326

I spent hours trying all the methods listed in that issue and the only one that worked was installing husky@4.2.3 which fixed the problem immediately, leading me to believe that there was a bug introduced in version 5. Please let me know if you need any other details.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 18 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @DouglasDev,

It’s normal behaviour with v5. You need to run npx husky install and create hooks with npx husky add .husky/pre-commit ....

You can check that it’s working by creating a new folder and running the following commands:

$ cd /tmp
$ mkdir test && cd test
$ git init && npm init -y && npm install husky -D

$ npx husky install
$ npx husky add .husky/pre-commit "echo hello"
$ git add package.json
$ git commit -m "test: hello should be printed"

@typicode Thanks for your reply. I will take your word for it that it works, but that seems like this change in the installation process will confuse many developers since none the dozens of tutorials on the internet on how to add husky to a project (for example the create react app docs and the prettier docs) mention this. I guess this change is necessary for the improved performance/memory usage? Perhaps it would be helpful to show these instructions in the terminal after you install husky?

I added backslash as a workaround. npx husky add .husky/pre-commit \“npm run test\”

but it doesn’t work if it is put in package.json

In windows environments the same problem exists (see #871). In that case it can be solved by calling the locally installed version of husky (.\node_modules\.bin\husky add .husky/pre-commit "echo hello"). Seems to me like calling husky with npx has a path problem.

Same issue here, we’re still using npm@6 on Windows 10 and the command npx --no is not yet available. On my side, I added 2 scripts in my “package.json” file :

"scripts": {
  "commitlint": "commitlint",
  "lint-staged": "lint-staged",
}

And thanks to that, my 2 files (with shebang as proposed here: https://github.com/typicode/husky/issues/864#issuecomment-778857821) are now triggered when I do git commit -m "my commit message"

“.husky/commit-msg”:

#!/usr/bin/env bash

npm run commitlint -- --edit $1

“.husky/pre-commit”:

#!/usr/bin/env bash

npm run lint-staged

If it can help someone 😊

Thanks for this upgrade @typicode !

I have the same issue! I upgraded to husky@5.0.9 and just noticed my pre-commit and pre-push hooks are not being triggered.

They worked well with husky@4.3.8.

my husky.config.js

module.exports = {
  hooks: {
    'pre-commit': lint-staged && npm run test:changed'])
  }
};