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

Hooks not working #326

Closed
standayweb opened this issue Aug 14, 2018 · 91 comments
Closed

Hooks not working #326

standayweb opened this issue Aug 14, 2018 · 91 comments

Comments

@standayweb
Copy link

I have set up husky commit hooks to run on commit and push, it works for me and another dev, but for one dev the commit hooks do not get run. Not sure what the issue is here

@andrea11
Copy link

I have the same issue... I don't know why but it seems that husky don't install correctly git hooks

@aguynamedben
Copy link

Same here

@aguynamedben
Copy link

I was experiencing this with a pre-push hook until I saw this other ticket in which somebody mentioned needing to use version 1.0.0-rc.13, then it worked.

@standayweb
Copy link
Author

Unfortunately, we are already using 1.0.0-rc.13 but thanks for the suggestion

@ramakrishnasomepalli
Copy link

I have the same issue i tried both the versions (1.0.0-rc.13 and the stable 0.14.3 ) but no hook (pre-commit,post-push ..etc) is triggering . I verified the hooks folder and all the respective scripts are available and husk is also installed as expected. I also verified the " npm config get ignore-scripts" and the flag is set to false.
Any suggestion how to make it work.

@nikolay-borzov
Copy link

I know that hooks don't work on WebStorm IDE because of the way how WebStorm uses Git

@yiuyiu
Copy link

yiuyiu commented Aug 30, 2018

@ramakrishnasomepalli
hello i met the same problem.And i find that the husky must install after git init . When installed it will show setting up git hooks ~

@typicode
Copy link
Owner

Have you fixed your issue?
Are you getting any output from husky when running npm install husky@next -D or when commiting/pushing with Git? Also which Git client or IDE are you using?

@ramakrishnasomepalli you've checked the right things... maybe I would check the value of git config core.hooksPath

@ramakrishnasomepalli
Copy link

I am still facing the error but the same configurations are working fine in another machine.

@ramakrishnasomepalli
Copy link

@typicode When i am committing i am not getting any logs related to husk.

@typicode
Copy link
Owner

@ramakrishnasomepalli when running git config core.hooksPath what do you get? When checking .git/hooks/pre-commit (or the failing hook) does it start with:

#!/bin/sh
# husky
# v1.0.0...

@Undistraction
Copy link

Undistraction commented Sep 11, 2018

I have the same problem. Hooks do not run. git config core.hooksPath returns nothing. I can't see anything in the docs. Does Husky expose a command to install the hooks if they fail on install?

@typicode
Copy link
Owner

typicode commented Sep 11, 2018

@Undistraction you can simply re-run npm install husky@next --save-dev

If it shows something different than the outpout below, could you copy/pate it here please?

husky > setting up git hooks
husky > done

@Undistraction
Copy link

Undistraction commented Sep 11, 2018

@typicode I found the problem. Husky is a dependency of another package. It seems that even though it is installed when I install that package, it doesn't set up the git hooks. This only happens when I install explicitly using npm i husky@next -D.

@typicode
Copy link
Owner

@Undistraction thanks for the update, I was running out of ideas 😅

@ramakrishnasomepalli
Copy link

ramakrishnasomepalli commented Sep 11, 2018

@typicode Please find the below log.

$ npm i husky@next -D

> husky@1.0.0-rc.13 preuninstall D:\workspace\TW-Workspace\Q2A\svc-q2a-quote-pricecheck\node_modules\husky
> node husky uninstall

husky > uninstalling git hooks
husky > done

> husky@1.0.0-rc.14 install D:\workspace\TW-Workspace\Q2A\svc-q2a-quote-pricecheck\node_modules\husky
> node husky install

husky > setting up git hooks
husky > done
npm WARN svc-q2a-quote-pricecheck@1.0.9 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ husky@1.0.0-rc.14
added 2 packages and updated 1 package in 203.131s.

And it is still not working also as suggested by @Undistraction I have noticed the below packages are missing from being installed under nodule modules of husky but the same are available under my project node modules.
"cosmiconfig": "^5.0.6","get-stdin": "^6.0.0","please-upgrade-node": "^3.1.1","run-node": "^1.0.0",

@Undistraction
Copy link

@typicode Thanks for all your help. Can you confirm that husky should install hooks if installed via the dependency of another package?

For example if my project installed a package called 'alpha' and that package has a dependency on 'husky', should husky install the githooks for my project? Or will this only happen if husky is installed directly in the project?

@standayweb
Copy link
Author

The developer who was having this issue is no longer at the company and has reset his laptop, I'll leave this open though as other people seem to be having the issue.

@MincheolC
Copy link

MincheolC commented Sep 20, 2018

Hi, I encounter with similar issue. I'm using husky v1.0.0-rc.14 and package.json as follow.

"husky": {
    "hooks": {
      "pre-push": "npm test"
    }
  }

pre-push hook doesn't not working, but pre-commit work fine as follow

"husky": {
    "hooks": {
      "pre-commit": "npm test"
    }
  }

Is there anyone here who can help me with this problem?? Thanks

p.s I want to use pre-push for unit test and pre-commit for lint.

@typicode
Copy link
Owner

Hi @MincheolC, can you check that .git/hooks/pre-commit and .git/hooks/pre-push have the same content?

@jakewies
Copy link

jakewies commented Oct 1, 2018

I seem to be having the same issue when upgrading from 0.14 to 1.0.1. I am using husky in conjunction with lint-staged:

// package.json

"husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "src/**/*.{js,jsx,json,css}": [
      ...
    ]
  }

I noticed that I am no longer getting any husky related output when committing.

My /.git/hooks/pre-commit contents begin with:

// .git/hooks/pre-commit

#!/bin/sh
# husky
# v1.0.1 darwin

...

Not sure what's going on, but I hope this info helps narrow the issue a bit. 😄

EDIT:

I also attempted to re-install husky via yarn add husky (installs v1.0.1) but never received any output regarding hooks being installed 🤷‍♂️

@MincheolC
Copy link

Hi, @typicode I just have pre-commit.sample and pre-push.sample in .git/hooks.
Do I need pre-commit and pre-push files?
If they're needed, then could you give me the content of them?

thanks~

@typicode
Copy link
Owner

typicode commented Oct 4, 2018

Hi @MincheolC,

Yes, they're needed. They should be created automatically for you when you run npm install husky --save-dev, or just with npm install if husky is already defined in package.json.

I'd suggest running npm install husky --save-dev and checking .git/hooks content again. Let me know if it creates them.

@typicode
Copy link
Owner

typicode commented Oct 4, 2018

Hi @jakewies,

To confirm that pre-commit is running, I'd try to edit .git/hooks/pre-commit and add something like:

#!/bin/sh
# husky
# v1.0.1 darwin
echo "pre-commit" # should be visible in the console when doing a git commit

...

@MincheolC
Copy link

@typicode thanks it works well. now~

@jakewies
Copy link

jakewies commented Oct 8, 2018

@typicode I'll update that file and see what happens 👍

@jakewies
Copy link

jakewies commented Oct 8, 2018

@typicode I updated .git/hooks/pre-commit with the line below:

 #!/bin/sh
 # husky
 # v1.0.1 darwin

echo "pre-commit"

...

I added a test file and committed to source. Unfortunately "pre-commit" was not logged to the console when running git commit. 😞

@typicode
Copy link
Owner

typicode commented Oct 8, 2018

@standayweb AFAICT it doesn't seem to be related to husky. Do you have a GIT_DIR env variable set?

@standayweb
Copy link
Author

@typicode The developer who was having this issue is no longer at the company and has reset his laptop

@typicode typicode closed this as completed Dec 7, 2018
@luisFebro
Copy link

luisFebro commented Feb 13, 2021

I did it using the husky@4.2.3 version

First I uninstalled the installed version> npm uninstall husky
I deleted the hooks folder inside the .git folder

then I installed the version mentioned above> npm i -D husky@4.2.3

With that I could see the messages:
husky> Setting up git hooks
husky> Done

The hooks folder in .git were created correctly and this time with the husky.sh file
And it worked for me.

This was the solution to me after a battle of tries. The current 5.0.9 version did not work to me...

@AbnerPS
Copy link

AbnerPS commented Feb 13, 2021

@typicode Eu encontrei o problema. Husky é uma dependência de outro pacote. Parece que, embora seja instalado quando instalo o pacote, ele não configura os ganchos do git. Isso só acontece quando eu instalo explicitamente usando npm i husky@next -D.

If you have trouble installing husky@next, try installing version 4 directly: npm install husky@4 --save-dev

@aymkin
Copy link

aymkin commented Feb 16, 2021

cd .git && rm -rf hooks
npm i -D husky@4.3.8
npm i -D husky

@antoniojps
Copy link

Version 4.2.3 fixed my issue. Works with yarn aswell.

@tarunbhartiya7
Copy link

Version 4.2.3 fixed my issue. Works with yarn aswell.

4.3.8 should also work.

@MeganRook18
Copy link

To get husky to work for me, I had to uninstall version 5 of husky and run npm install husky@4 --save-dev, then run npm rebuild.

@samer-nowrx
Copy link

samer-nowrx commented Mar 4, 2021

Yes! Apparently version 5 is only available to use in open-source projects! Version 4 is free to use for everyone!

Also, I had to npm install husky@4 -g globally. Good luck!

@num8er
Copy link

num8er commented Mar 11, 2021

I solved it simply calling install from husky binary

 ./node_modules/.bin/husky install

@ghost
Copy link

ghost commented Mar 28, 2021

I had to remove husky and then add it again on version 4.3.8 and worked just fine

@callum-bennett
Copy link

It looks like from Husky version 5 onwards you no longer add the husky pre-commit command to the package.json file.

I removed the following from package.json

"husky": {
   "hooks": {
      "pre-commit": "lint-staged"
   }
 }

Then ran the following

yarn remove husky
yarn add -D husky
husky install
husky add .husky/pre-commit "lint-staged" 
chmod a+x .husky/pre-commit

Hope this helps

@dreadwail
Copy link

If you've tried everything listed in this issue and still can't get the damn hooks to install while using older versions of husky (I was using 3.0.8), just bypass all the cruft and run this:

$ node ./node_modules/husky/lib/installer/bin.js install

No idea why it wouldn't run for me during installation of the husky package, but that did the trick for me.

husky > Setting up git hooks
husky > Done

Seems pretty brittle, but I guess 3.0.8 is a pretty old version at this point so maybe the newer versions are a bit better.

@Dinika
Copy link

Dinika commented Apr 18, 2021

It looks like from Husky version 5 onwards you no longer add the husky pre-commit command to the package.json file.

I removed the following from package.json

"husky": {
   "hooks": {
      "pre-commit": "lint-staged"
   }
 }

Then ran the following

yarn remove husky
yarn add -D husky
husky install
husky add .husky/pre-commit "lint-staged" 
chmod a+x .husky/pre-commit

Hope this helps

This works for me with the expectation that I had to use yarn to run the lint-staged command for me in pre-commit (since I have not installed lint-staged globally in my laptop):

husky add .husky/pre-commit "yarn add lint-staged" 

I also did not have to make the .huskypre-commit executable. In the husky version I installed, it's executable by default.

@jhinter
Copy link

jhinter commented Apr 27, 2021

None of the above answers were working for me!

But something finally solved the problem:
I just checked out my repo again, and husky is finally working again! (4.3.6) 🎉

There has to be something messed up in .git, but I don't know what it is. Removing .git/hooks was not resolving the problems for me.

@MatteoInfi
Copy link

npm uninstall husky
npm install -D husky

This is bug on the husky side

@JanithaR
Copy link

None of the above answers were working for me!

But something finally solved the problem:
I just checked out my repo again, and husky is finally working again! (4.3.6) 🎉

There has to be something messed up in .git, but I don't know what it is. Removing .git/hooks was not resolving the problems for me.

Well, I'll be damned. Busted my brain for hours and finally came down this thread enough to read this comment and tried. Everything started working as expected. v4.3.8

@DanielRuf
Copy link

If you downgrade from a newer version to v4, ensure to run git config --unset core.hooksPath. Because git config core.hooksPath will probably display .husky but should be .git.

@tomatobybike
Copy link

yarn remove husky

yarn add -D husky@4.3.8

and then you can do it success

@zedfight
Copy link

zedfight commented Sep 1, 2021

Delete project and re-pull remotely

@javed7891
Copy link

I can confirm there is a serious bug in version 5. I spent hours trying all the methods and the only one that worked was installing husky@4.2.3. I'm on mac with git version 2.20.1, node v14.15.4, and npm v6.14.10. This issue should be reopened.

Thanks for sharing the fix, this is the only version that worked for macos.

@atul161
Copy link

atul161 commented Oct 14, 2021

Hii @xenoterracide @aguynamedben @Undistraction @pke @samer-nowrx @sapegin @pke @samer-nowrx

Documentation of husky is telling that you automatic configure husky but that is not working correctly.

I have figured it out. You neeed unisntall the husky first using command npm uninstall husky and after that again install it using command npm install husky --save-dev . Now run the listed command

  • npx husky install
  • npm set-script prepare "husky install"
  • npx husky add .husky/pre-commit "Add your coomand here ie lint staged npm test etc"
  • If you want to add pre-push then run npx husky add .husky/pre-push "your command here ie. npm run type-check"
  • Now add the following thing into package.json
 "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "pre-push": "npm run type-check"
    }
  • If you are using husky for linter then add this also in package.json
  "lint-staged": {
    "*.@(ts|tsx)": [
      "npm run lint",
      "npm run  format"
    ]
  }
  
  • Now try to commit . Note that dont add .husky into gitignore because sometimes husky will not work for other developer and if .husky folder is present same for every developer then it will work correctly.
  • If you get an error like husky not found then go to package.json and remove prepare husky install and run npm install again

@Marceloasf
Copy link

What worked for me was:

  • Make sure Husky v4 is installed (v7 was not working).
  • Check the output of git config core.hooksPath. This should not return anything. If it does run the command:

git config --unset core.hookspath

@kostyanp95
Copy link

What worked for me was:

  • Make sure Husky v4 is installed (v7 was not working).
  • Check the output of git config core.hooksPath. This should not return anything. If it does run the command:

git config --unset core.hookspath

The only thing that helped me out of all the above options! Thanks!

@xiaoyao316
Copy link

I found that when git pull is executed, if there is no updatable content, the post merge hook will not be triggered, but if there is updatable content, it will be triggered.

@jdeblasse
Copy link

@kostyanp95 and others in this thread. The problem is likely due to file permissions of your hooks. Please check.

File mode changed from 100644 (Regular) to 100775 (Executable)

@typicode
Copy link
Owner

I'm going to lock this issue, since it goes back to 2018 and solutions provided at the beginning may not be relevant anymore. I think it can create more confusion and harder to debug to leave multiples issues/versions in a single thread.

Feel free to create new issues if needed.

Thanks.

Repository owner locked as resolved and limited conversation to collaborators Feb 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests