Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lint-staged not running on precommit

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"
},
like image 354
Andrew Horn Avatar asked Apr 26 '18 17:04

Andrew Horn


People also ask

Why is lint staged?

Working on staged files limits the number of files you need to lint at any given time and makes the workflow faster. The commands you configure will run "pre-commit".

How do I disable my husky?

You can set HUSKY environment variable to 0 in your CI config file, to disable hooks installation. Alternatively, most Continuous Integration Servers set a CI environment variable. You can use it in your hooks to detect if it's running in a CI.

Is Git add required in Lint stage?

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.

How do I install Husky and lint-staged in my project?

Make sure Prettier is installed and is in your devDependencies before you proceed. This will install husky and lint-staged, then add a configuration to the project’s package.json that will automatically format supported files in a pre-commit hook. Read more at the lint-staged repo.

How to commit changes to a lint-staged Repo?

'lint-staged' is not recognized as an internal or external command, operable program or batch file. (Windows) Insure you are on a Windows machine with git installed. Make a change to your repo. Using Visual Studio Code or simply cmd.exe, attempt to commit your changes.

What happens when Lint-staged fails to work?

If the linter cannot make the fix then lint-staged will bail out, preventing the commit being made, and printing the linter’s output to the console to allow manual fixes to be made. A reminder here that this example has eslint installed in the root.


17 Answers

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
like image 141
Nisharg Shah Avatar answered Oct 05 '22 20:10

Nisharg Shah


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!

like image 24
Swaathi Kakarla Avatar answered Oct 05 '22 21:10

Swaathi Kakarla


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

like image 29
Juanma Menendez Avatar answered Oct 05 '22 20:10

Juanma Menendez


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

like image 21
Andrew Horn Avatar answered Oct 05 '22 21:10

Andrew Horn


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
like image 24
Ankur Marwaha Avatar answered Oct 05 '22 19:10

Ankur Marwaha


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.

like image 20
Danna Avatar answered Oct 05 '22 21:10

Danna


You are missing dependencies:

npm install --save-dev prettier husky lint-staged
like image 20
Mindaugas Jaraminas Avatar answered Oct 05 '22 20:10

Mindaugas Jaraminas


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

like image 39
Geoff Davids Avatar answered Oct 05 '22 20:10

Geoff Davids


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
like image 38
Black Mamba Avatar answered Oct 05 '22 21:10

Black Mamba


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",
...
like image 33
W.Perrin Avatar answered Oct 05 '22 20:10

W.Perrin


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.

like image 29
Frondor Avatar answered Oct 05 '22 21:10

Frondor


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
like image 33
Fatih Bulut Avatar answered Oct 05 '22 20:10

Fatih Bulut


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.

like image 32
squillman Avatar answered Oct 05 '22 20:10

squillman


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.

like image 1
Aanchal1103 Avatar answered Oct 05 '22 19:10

Aanchal1103


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

like image 1
Luis Febro Avatar answered Oct 05 '22 20:10

Luis Febro


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 this

like image 1
Ayush Shankar Avatar answered Oct 05 '22 19:10

Ayush Shankar


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

chmod +x .husky/pre-commit
like image 1
Tamlyn Avatar answered Oct 05 '22 21:10

Tamlyn