lint-staged not running on precommit
Solution 1
Reinstalled husky and now seems to be working. Thanks @mpasko256 for your help!
Solution 2
In 2021
Sometimes hooks are not added by husky
so you need to add it using a simple easy hack.
You need to uninstall husky first after that install V4
of husky because it ensures that your hooks are correctly installed and after that install the latest version of husky
so you get the latest updates.
NPM
npm uninstall husky
npm install -D husky@4
npm install -D husky
YARN
yarn remove husky
yarn add -D husky@4
yarn add -D husky
If sometimes above trick not works, so let's add the hook into husky, below mention method is used only in V6
and I am showing the husky
with lint-staged
example.
NPM
npm install -D husky
npm set-script prepare "husky install" && npm run prepare
npx husky add .husky/pre-commit "npx lint-staged"
git commit -m "added husky and lint-stagged" // here you will notice the lint-staged checking the files with help of husky
YARN
yarn add -D husky
npm set-script prepare "husky install" && yarn prepare
npx husky add .husky/pre-commit "yarn lint-staged"
git commit -m "added husky and lint-stagged" // here you will notice the lint-staged checking the files with help of husky
Solution 3
I tried so many solutions on here but a combination finally worked!
- Make sure Husky v4 is installed. v6 was never triggering for me.
- Check the output of
git config core.hooksPath
. This should not return anything. If it does run,
git config --unset core.hookspath
And FINALLY it worked!
Solution 4
The problem for me was I ran "npx mrm lint-staged" like the official website says but it only set the husky and lint-staged configurations in package.json. It does not add then as dependency or installed them.
The solution for me was:
npm i -D husky lint-staged
npx mrm lint-staged
Solution 5
For me the issue was resolved by uninstalling and installing lower version
npm uninstall husky
npm install -D husky@4 //after this it will work
Related videos on Youtube
Andrew Horn
I work with React on the front end and Node on the backend.
Updated on July 08, 2022Comments
-
Andrew Horn almost 2 years
prettier is not running on precommit. This worked with the same configuration in other projects, so I'm baffled why it's not working this time.
This is the relevant section of my package.json file:
"scripts": { "precommit": "lint-staged" }, "lint-staged": { "*.{js,json,css,scss,html,md}": [ "prettier --write", "git add" ] },
Edit. Here are the relevant devDependencies:
"devDependencies": { "husky": "^0.14.3", "lint-staged": "^7.0.4", "prettier": "1.12.0" },
-
mpasko256 about 6 yearsThe configuration must be indeed different as it is not going to work but to help, we need more information. Do you have some error messages/logs? Is prettier installed properly on current project? Does
git add
work properly instead? -
mpasko256 about 6 yearsDo you have husky installed? github.com/typicode/husky How about option 5? prettier.io/docs/en/precommit.html
-
Andrew Horn about 6 years@mpasko256 Yes, I have husky installed. No error messages popping up. Prettier is installed properly. I will paste the relevant section of devDependencies
-
Andrew Horn about 6 years@mpasko256 option 5 is a bit confusing... How would I make it work for files other than js files?
-
mpasko256 about 6 yearsI meant to try out option 5 only for test purposes. But to make it less confusing for you: it simply queries git for changed files and saves result in $jsfiles variable. You can just change
"*.js" "*.jsx"
into"*.js" "*.json" "*.css" "*.scss" "*.html" "*.md"
to make it work for your example.
-
-
Rupesh Kumar Tiwari almost 6 yearsHey I had already installed lint-staged and prettier however I did not installed husky since i thought I am not using it so why to install. But I am not sure the moment I just installed husky lint-staged started working Thanks. !
-
Aanchal1103 over 4 yearsIt does, for me.
-
n8jadams about 4 yearsSurprised to see an answer so recent. I think my machine was in a funk and I had the problem OP had. Thanks for the answer, it seemed to do the trick for me. Thanks!
-
Danna about 4 yearsAha, I just got a similar issue recently, thank you as well!
-
Liam Kernighan almost 4 yearsthank you. yarn rebuild is has not been found in my system, so needed just to yarn remove husky && yarn add --dev husky
-
jjalonso almost 4 years▶ yarn rebuild yarn run v1.3.2 (node:99635) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. error Command "rebuild" not found.
-
Yohan Dahmani almost 4 yearsit fixed my issue
-
Radovan Babjak over 3 yearsThanks for sharing. What helped me was the 2nd and 3rd step and did not need to downgrade my NPM.
-
dspacejs about 3 years
yarn rebuild
isn't a command (or at least it's not anymore). See: github.com/yarnpkg/yarn/issues/2069 -
rainversion_3 about 3 yearsAs @Geoff mentioned in below answer, hooks aren't automatically installed using
husky@4
, pre-commit hooks work. This should be the current accepted answer -
kevin almost 3 yearsYep, same thing happened to me and this solution works.
-
spierala almost 3 yearsYes, this works. I had downgraded from husky 5 to 4. But v4 was not working - although the same setup worked in another project. This solution was the missing step.
-
meowww almost 3 yearsI can't believe it, it works so well, thank you so much My husky 3.0.4
-
Rajiv almost 3 yearsit really worked.. any explanation why it worked?
-
jcollum almost 3 yearsfor
npm set-script prepare
I getsh: husky: command not found
-- do you have it installed as a global? -
Nisharg Shah almost 3 yearsno, it will automatically be stored in the
.bin
folder and it will execute from there. -
jcollum almost 3 yearsAh, husky 4 won't work for that, that was the issue. Been downgrading to 4 and then installing 7 to try and sort out these issues. Had 4 installed.
-
Nisharg Shah almost 3 yearsI tried and it is working on my system and I didn't install the global package, are you using linux?
-
Cosmin Stoian over 2 yearsSaved my day! Ran
npx mrm lint-staged
then the hooks worked like a charm. -
João Ignacio over 2 yearsReally nice man. Kudos!
-
Swaathi Kakarla over 2 yearsThanks @JoãoIgnacio – but I'm not a man. :')
-
João Ignacio over 2 yearsSo sorry @SwaathiKakarla! Thank you ma'am!
-
Emad Baqeri over 2 yearsI think this trick is not working these days, I have done this by clearing my cache but did not worked
-
katerlouis over 2 yearsThis now even works with [email protected] for me! NICE
-
Bi Wu over 2 yearsIt worked like a charm. Thank you!
-
JanBrus over 2 yearsReason why this works is that husky versions after 4 stopped installing git hooks automatically. They put up instructions how to add those here github.com/typicode/husky
-
Hans about 2 yearsI had been trying to fix this for a couple of hours, thanks!