husky v9 lint-staged not triggered on pre-commit
Context Please describe your issue and provide some context:
- Mac
- Iterm and VSCode
- Hook doesn't run
husky 9.0.11
lint-staged 15.2.2
Used https://github.com/uulm-mrm/git_hooks to get a basic husky+lint-staged+prettier+eslint setup.
If I run npx lint-staged myself in a terminal I see the expected scripts run but committing doesn't trigger the scripts and allows the commit to go through.
Running git config core.hooksPath outputs .husky/_
git --version output git version 2.45.1
troubleshooting section says to be on git 2.9 which feels like a mistake as 2.45.1 is latest
package.json
"scripts": {
....
"prepare": "husky"
},
...
"lint-staged": {
"*.js": "eslint --cache --fix",
"*.{js,mjs,css,md,hbs}": "prettier --write"
}
.husky/pre-commit
npx lint-staged
I also had the same problem
Any chance your package.json isn't in the root directory of your repo?
I thought mine was broken (which is how I came across this issue), but then I found an adjusted config from a similar repo, and it's working fine now. My change is just "prepare": "cd .. && husky", but that may be irrelevant for you 🤔
I also had the same problem
I have been facing the same issue where Husky's execution suddenly stops working without apparent reason.
After investigating in my repository, I noticed that this error occurs in all Husky hooks, specifically when the following scenario happens:
- I initialize a project with Git using
git init. - I install Husky in the project following the standard recommendation.
- I delete the
.gitfolder from the project using the commandrm -rf .git. - I reinitialize Git in the project with
git init. - I try to execute a Husky hook, but I observe that it stops working immediately.
To temporarily solve this issue, the only effective measure I have found is to reinstall Husky every time the .git folder is deleted.
I hope these detailed steps clarify the issue I have been experiencing with Husky in my repository.
Happens when I manually migrated v8 -> v9
Seems like the problem is Linux\Windows separator issue.
As part of migration Git config (located in .git/config file) has been changed, to reflect husky's simplified hooks path
before
[core]
hooksPath = .husky
after
[core]
hooksPath = .husky\\_
So if I change hooks path to .husky/_ - everything started to work again
Same issue. I've tried every solution that exists on the internet and it still doesn't work when I commit.
I couldn't get lint-staged to work with husky in my project, so I added a custom command to run lint and format in my project which now works when I make a commit. This is my config settings
{
"scripts": {
// other commands
"lint": "eslint .",
"prettier:format": "prettier --check . || prettier --write .",
"preview": "vite preview",
"prepare": "husky"
},
And my pre-commit hook
set -e
npm run prettier:format || true
npm run lint
[!note] The
-eflag allows the hook to only terminate when an error occurs (Mandatory to allow both commands to run). Here's the result.
https://github.com/user-attachments/assets/4294ecf3-1ab3-4bff-928f-7e23f2c815bf
It's not the cleanest solution but it's better than nothing at this point. Hopefully this could help someone.
I am having the same issue but only when committing from VS Code. It appears it is using an older version of node, which I have no idea where it pulls it from.