Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git commit hooks never get created when husky v5 is installed #866

Closed
DouglasDev opened this issue Feb 12, 2021 · 18 comments
Closed

git commit hooks never get created when husky v5 is installed #866

DouglasDev opened this issue Feb 12, 2021 · 18 comments

Comments

@DouglasDev
Copy link

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: #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.

@typicode
Copy link
Owner

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"

@jimyhdolores
Copy link

@typicode the command you provide does not work

image

version husky ^5.0.9

@DouglasDev
Copy link
Author

DouglasDev commented Feb 12, 2021

@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?

@wendelsantosd
Copy link

husky@4.2.3 works

@csantos1113
Copy link

csantos1113 commented Feb 14, 2021

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'])
  }
};

@krisgerhard
Copy link

Could be related to #869. Try the fix provided in this issue.

@galilmori
Copy link

I have the same issue since i upgraded to 5.0.9, i am using lint-staged 10.5.4

@BePo65
Copy link

BePo65 commented Feb 16, 2021

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.

@Pezmc
Copy link

Pezmc commented Feb 17, 2021

@typicode I think shome people are likely just missing that Husky 5 doesn't read the config found in package.json anymore, and that's muddying the waters on these threads (as per comment & comment)

For anyone using husky 5, to setup lint-staged with husky 5, just run:

npx husky add .husky/pre-commit "yarn run lint-staged"
# or
npx husky add .husky/pre-commit "npx lint-staged"

And drop the husky section from your package.json.
See: https://typicode.github.io/husky/#/?id=migrate-from-v4-to-v5

@BePo65
Copy link

BePo65 commented Feb 17, 2021

for me the problem was npx. I am on a windows 10 machine with node LTS (V14 with npm v6).
Using the command from the documentation (the same as @typicode mentioned above) just resulted in the husky help in my command window. Only using the husky installation in my project solved the issue (.\node_modules\.bin\husky add .husky/pre-commit "echo hello").

That the husky configuration change with V5 is clearly described in the documentation (and has a longer discussion in an issue :-)

@gmonte
Copy link

gmonte commented Feb 17, 2021

I have the same issue with node v12.20.1, yarn v1.22.10, macOS v11.2.
Husky v4.2.3 work fine to me.

@typicode
Copy link
Owner

Apparently it's a bug with npx on Windows, it seems to be fixed in npm v7.5.4

@SuperITMan
Copy link

SuperITMan commented Feb 20, 2021

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: #864 (comment)) 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 !

@jordanbtucker
Copy link

@typicode Can you point me to the bug in npx?

infotexture added a commit to dita-ot/docs that referenced this issue Mar 2, 2021
https://typicode.github.io/husky/#/?id=migrate-from-v4-to-v5

Additional changes:

- Use prepare script instead of postinstall:
  typicode/husky#890
- Remove old package.json > husky field
- Add `lint-staged` to package scripts & prefix `pre-commit` with npx
  typicode/husky#866 (comment)

Signed-off-by: Roger Sheen <roger@infotexture.net>
@typicode
Copy link
Owner

typicode commented Mar 3, 2021

@typicode Can you point me to the bug in npx?

@jordanbtucker don't have a link sorry. However a temporary fix was provided here before the official one #873 (I did some tests too, and I think it's fixed in npm v7.4).

@typicode
Copy link
Owner

typicode commented Mar 3, 2021

Recently, a new command has been introduced to simplify setup.

You can now just run:

npm i husky -D
npx husky init

package.json will be automatically updated, a sample pre-commit will be created and configured to run npm test on pre-commit.

I hope it makes things more straightforward :)

Docs have been updated. Also if you're using npm, pnpm or yarn v1, you can just have prepare script instead of the more complex postinstall [ + pinst ] combo.

infotexture added a commit to dita-ot/website that referenced this issue Mar 4, 2021
https://typicode.github.io/husky/#/?id=migrate-from-v4-to-v5

Additional changes:

- Use prepare script instead of postinstall:
  typicode/husky#890
- Remove old package.json > husky field
- Add `lint-staged` to package scripts & prefix `pre-commit` with npx
  typicode/husky#866 (comment)

Signed-off-by: Roger Sheen <roger@infotexture.net>
@typicode typicode closed this as completed Mar 7, 2021
@leowongcanto
Copy link

leowongcanto commented Apr 25, 2021

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

@nguyentrancong
Copy link

nguyentrancong commented Nov 16, 2023

  1. npx husky install
  2. npx husky add .husky/pre-commit "npm run lint-staged"

I did it, It works for me, Maby it helps you. lucky
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.