lint-staged not running on precommit

106,282

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!

  1. Make sure Husky v4 is installed. v6 was never triggering for me.
  2. 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:

  1. npm i -D husky lint-staged

  2. 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
Share:
106,282

Related videos on Youtube

Andrew Horn
Author by

Andrew Horn

I work with React on the front end and Node on the backend.

Updated on July 08, 2022

Comments

  • Andrew Horn
    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
      mpasko256 about 6 years
      The 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
      mpasko256 about 6 years
      Do you have husky installed? github.com/typicode/husky How about option 5? prettier.io/docs/en/precommit.html
    • Andrew Horn
      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
      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
      mpasko256 about 6 years
      I 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
    Rupesh Kumar Tiwari almost 6 years
    Hey 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
    Aanchal1103 over 4 years
    It does, for me.
  • n8jadams
    n8jadams about 4 years
    Surprised 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
    Danna about 4 years
    Aha, I just got a similar issue recently, thank you as well!
  • Liam Kernighan
    Liam Kernighan almost 4 years
    thank you. yarn rebuild is has not been found in my system, so needed just to yarn remove husky && yarn add --dev husky
  • jjalonso
    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
    Yohan Dahmani almost 4 years
    it fixed my issue
  • Radovan Babjak
    Radovan Babjak over 3 years
    Thanks for sharing. What helped me was the 2nd and 3rd step and did not need to downgrade my NPM.
  • dspacejs
    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
    rainversion_3 about 3 years
    As @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
    kevin almost 3 years
    Yep, same thing happened to me and this solution works.
  • spierala
    spierala almost 3 years
    Yes, 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
    meowww almost 3 years
    I can't believe it, it works so well, thank you so much My husky 3.0.4
  • Rajiv
    Rajiv almost 3 years
    it really worked.. any explanation why it worked?
  • jcollum
    jcollum almost 3 years
    for npm set-script prepare I get sh: husky: command not found -- do you have it installed as a global?
  • Nisharg Shah
    Nisharg Shah almost 3 years
    no, it will automatically be stored in the .bin folder and it will execute from there.
  • jcollum
    jcollum almost 3 years
    Ah, 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
    Nisharg Shah almost 3 years
    I tried and it is working on my system and I didn't install the global package, are you using linux?
  • Cosmin Stoian
    Cosmin Stoian over 2 years
    Saved my day! Ran npx mrm lint-staged then the hooks worked like a charm.
  • João Ignacio
    João Ignacio over 2 years
    Really nice man. Kudos!
  • Swaathi Kakarla
    Swaathi Kakarla over 2 years
    Thanks @JoãoIgnacio – but I'm not a man. :')
  • João Ignacio
    João Ignacio over 2 years
    So sorry @SwaathiKakarla! Thank you ma'am!
  • Emad Baqeri
    Emad Baqeri over 2 years
    I think this trick is not working these days, I have done this by clearing my cache but did not worked
  • katerlouis
    katerlouis over 2 years
    This now even works with [email protected] for me! NICE
  • Bi Wu
    Bi Wu over 2 years
    It worked like a charm. Thank you!
  • JanBrus
    JanBrus over 2 years
    Reason 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
    Hans about 2 years
    I had been trying to fix this for a couple of hours, thanks!