lint-staged not running on precommit

JavascriptGitPre Commit-HookPrettierLint Staged

Javascript Problem Overview


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"
},

Javascript Solutions


Solution 1 - Javascript

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 2 - Javascript

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 3 - Javascript

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 4 - Javascript

Reinstalled husky and now seems to be working. Thanks @mpasko256 for your help!

Solution 5 - Javascript

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

Solution 6 - Javascript

Probably your husky package already in your node_modules before you configured this script. Try to reinstall the hooks, you can run:

npm rebuild

Or if you're using yarn:

npm rebuild --update-binary

It solved my problem.

Solution 7 - Javascript

You are missing dependencies:

npm install --save-dev prettier husky lint-staged

Solution 8 - Javascript

For anyone with this problem and using Husky 5, the hooks aren't automatically installed. So you probably just don't have the required hooks in your .git/hooks folder at all. You need to either add a postinstall to your package.json (recommended), or run npx husky install after you've npm installed the package.

enter image description here

Or just downgrade to Husky 4. You'll actually have to do this, if, like me, you're working on a commercial project and don't want to be a Husky sponsor.

enter image description here

https://dev.to/typicode/what-s-new-in-husky-5-32g5

Solution 9 - Javascript

Wasted hours in figuring out the cause and using the solutions above

Read the documentation and avoid googling: https://typicode.github.io/husky/#/?id=automatic-recommended

Or follow the steps below: > husky-init is a one-time command to quickly initialize a project with husky.

npx husky-init && npm install       # npm
npx husky-init && yarn              # Yarn 1
yarn dlx husky-init --yarn2 && yarn # Yarn 2

Solution 10 - Javascript

I think there was something wrong with your package.json.

"scripts":{
   ...
},
"husky": {
	"hooks": {
		"pre-commit": "lint-staged",
		"pre-push": "npm test"
	}
},
"lint-staged": {
    "*.ts": ["tslint", "prettier --write", "git add"]
}

By the way, after installed husky, just check .git/hooks/pre-commit content. If no husky like word in it, just remove the .git/hooks/pre-commit file and reinstall husky or run npx husky. Because husky will skip modifying the .git/hooks/pre-commit file if it is not GHook alike or PreCommit alike.

You may find it out by following this link. https://github.com/typicode/husky/blob/master/src/installer/hooks.ts#L58

One alternative is to use pre-commit.

yarn add --dev pre-commit
"scripts":{
   ...
},
"pre-commit":"lint-staged",
...

Solution 11 - Javascript

This was happening to me and none of these answers helped. So for future reference, it was because I was using npm@7 which looks like it doesn't know how to properly execute husky.

The way I found out it was a problem with husky and npm was because I found out that I had no pre-commit file inside my-project/.git/hooks directory.

When you install husky, it automatically do its magic for you in such folder. So for that, I had to:

  1. Downgrade to npm i -g npm@6
  2. Be sure everything was freshly reinstalled with rm -rf node_modules package-lock.json && npm i (you should see Husky output in the console)
  3. And although it isn't really needed, I executed again npx mrm lint-staged

Finally, it worked.

Solution 12 - Javascript

I solved my problem by adding yarn at the beginning of commands. (husky v6)

.husky/pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged

.husky/commit-msg

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn commitlint -e $HUSKY_GIT_PARAMS

Solution 13 - Javascript

In case it helps someone else: another thing to try is to delete your node_modules folder and rerun npm install

I originally ran npm install in the linux subsystem on my Windows 10 machine. Everything worked fine using git through bash. I received the error after switching over to git in Powershell. Uninstalling and reinstalling prettier, husky, and lint-staged did not work for me.

I deleted my node_modules folder and reran npm install from the Windows side and now it works.

Solution 14 - Javascript

The problem in my case was that there were some existing hooks and husky does not override them (more info here).

Just putting it here in case someone else runs into the same issue.

Solution 15 - Javascript

The git add command is no longer required in the lint-stage v10 onwards. It is automatically inserted to the commit as the docs describe it:

> From v10.0.0 onwards any new modifications to originally staged files will be automatically added to the commit. If your task previously contained a git add step, please remove this. The automatic behaviour ensures there are less race-conditions, since trying to run multiple git operations at the same time usually results in an error.

https://github.com/okonet/lint-staged#configuration

Solution 16 - Javascript

For windows users, simply do the following in command line/bash:

set HUSKY_DEBUG = 1

or

set HUSKY_DEBUG = true

This solved , my hours of head scratching.
Also see https://vision.middlebury.edu/~tdorjee/node_modules/husky/DOCS.md">this</a>

Solution 17 - Javascript

To follow @typicode's message here:

> I suspect it's because npm run modifies PATH to include node_modules/.bin. On the other side, when hook commands are run PATH isn't modified.

If you change your .husky/pre-commit to include this path, it works with husky@latest:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

node_modules/.bin/lint-staged

Solution 18 - Javascript

I had the same problem, but I did this mistake.

I have added lint-staged object inside husky object, but later realized I need to add lint-staged key-value pairs as direct key-value pairs in package.json

"lint-staged": {
  "*.{js,json,css,scss,html,md}": [
    "prettier --write",
    "git add"
  ]

Solution 19 - Javascript

Please pay attention to the node version you are using. Husky requires node >= 10 and lint-staged requires node >= 10.13

Solution 20 - Javascript

Make sure you installed husky

add the below scripts to package.json script

"prepare": "husky install && npx husky add .husky/pre-commit \"npm run lint-fix\"",
"lint": "eslint ./",
"lint-fix": "eslint ./ --fix"

you scripts will be looking something like this

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "prepare": "husky install && npx husky add .husky/pre-commit \"npm run lint-fix\"",
    "lint": "eslint ./",
    "lint-fix": "eslint ./ --fix",
    "format": "prettier --write \"**/*.{js,jsx,json,md}\""
  },

run the following command

npm run prepare 

this script will create a .husky folder to your working directory and adds pre-commit file to it with script npm run lint-fix to it.

congrats ... now you can commit your files and see precommit checks your eslint error if any

you can add below line to your .git ignore file

/.husky

Solution 21 - Javascript

For me the problem was that the pre-commit hook was not executable which was easily fixed:

chmod +x .husky/pre-commit

Solution 22 - Javascript

you can also use pre-commit library. You don't have to configure any thing, It works like charm.

How to use ⬇️

{
  "name": "437464d0899504fb6b7b",
  "version": "0.0.0",
  "description": "ERROR: No README.md file found!",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: I SHOULD FAIL LOLOLOLOLOL \" && exit 1",
    "foo": "echo \"fooo\" && exit 0",
    "bar": "echo \"bar\" && exit 0"
  },
  "pre-commit": [
    "foo",
    "bar",
    "test"
  ]
}

Solution 23 - Javascript

Breaking changes

Be aware that there were breaking changes in the 5x > version.

If you're struggling to get it working, here's how I got Husky(v6) working with lint-staged.

Assuming that you already have it installed otherwise skip to step number 3.

1 - yarn remove husky

2 - yarn add -D husky

3 - husky install

4 - husky add .husky/pre-commit "pre-comit"

5 - chmod a+x .husky/pre-commit

6 - In the package.json add the following script "pre-commit": "lint-staged"

7 - Add your lint-staged configuration e.g

...
"lint-staged": {
    "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
      "prettier --write"
  ]
}
...

Solution 24 - Javascript

I have the same problem for another reason. Just had the HKEY_CURRENT_USER \ Software \ Microsoft \ Command Processor \ AutoRun set to cd \python. After deleting this "AutoRun", lint-staged is running on precommit without any errors.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew HornView Question on Stackoverflow
Solution 1 - JavascriptNisharg ShahView Answer on Stackoverflow
Solution 2 - JavascriptSwaathi KakarlaView Answer on Stackoverflow
Solution 3 - JavascriptJuanma MenendezView Answer on Stackoverflow
Solution 4 - JavascriptAndrew HornView Answer on Stackoverflow
Solution 5 - JavascriptAnkur MarwahaView Answer on Stackoverflow
Solution 6 - JavascriptDannaView Answer on Stackoverflow
Solution 7 - JavascriptMindaugas JaraminasView Answer on Stackoverflow
Solution 8 - JavascriptGeoff DavidsView Answer on Stackoverflow
Solution 9 - JavascriptBlack MambaView Answer on Stackoverflow
Solution 10 - JavascriptW.PerrinView Answer on Stackoverflow
Solution 11 - JavascriptFrondorView Answer on Stackoverflow
Solution 12 - JavascriptFatih BulutView Answer on Stackoverflow
Solution 13 - JavascriptsquillmanView Answer on Stackoverflow
Solution 14 - JavascriptAanchal1103View Answer on Stackoverflow
Solution 15 - JavascriptLuis FebroView Answer on Stackoverflow
Solution 16 - JavascriptAyush ShankarView Answer on Stackoverflow
Solution 17 - JavascriptPhil GibbinsView Answer on Stackoverflow
Solution 18 - Javascriptmadhu131313View Answer on Stackoverflow
Solution 19 - Javascriptd_bhatnagarView Answer on Stackoverflow
Solution 20 - JavascriptAravind SiruvuruView Answer on Stackoverflow
Solution 21 - JavascriptTamlynView Answer on Stackoverflow
Solution 22 - JavascriptAquib AfzalView Answer on Stackoverflow
Solution 23 - JavascriptGilson VianaView Answer on Stackoverflow
Solution 24 - JavascriptEdward_GMView Answer on Stackoverflow