133

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"
},
Andrew Horn
  • 2,291
  • 2
  • 16
  • 26
  • 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 Apr 26 '18 at 17:30
  • Do you have husky installed? https://github.com/typicode/husky How about option 5? https://prettier.io/docs/en/precommit.html – mpasko256 Apr 26 '18 at 17:35
  • @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 Apr 26 '18 at 17:48
  • @mpasko256 option 5 is a bit confusing... How would I make it work for files other than js files? – Andrew Horn Apr 26 '18 at 17:58
  • 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. – mpasko256 Apr 26 '18 at 18:14

29 Answers29

95

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
Nisharg Shah
  • 16,638
  • 10
  • 62
  • 73
89

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!

Swaathi Kakarla
  • 2,227
  • 1
  • 19
  • 27
42

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

Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103
Juanma Menendez
  • 17,253
  • 7
  • 59
  • 56
28

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

Andrew Horn
  • 2,291
  • 2
  • 16
  • 26
28

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
Ankur Marwaha
  • 1,613
  • 2
  • 15
  • 30
  • 1
    Yep, same thing happened to me and this solution works. – kevin Jun 10 '21 at 15:53
  • 1
    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 – JanBrus Feb 09 '22 at 10:43
25

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.

Daniel
  • 3,115
  • 5
  • 28
  • 39
Danna
  • 279
  • 3
  • 4
  • 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! – n8jadams Mar 24 '20 at 15:37
  • Aha, I just got a similar issue recently, thank you as well! – Danna Mar 27 '20 at 02:18
  • 1
    thank you. yarn rebuild is has not been found in my system, so needed just to yarn remove husky && yarn add --dev husky – Liam Kernighan Jul 25 '20 at 08:20
  • ▶ 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. – jjalonso Aug 11 '20 at 15:23
  • it fixed my issue – Yohan Dahmani Aug 28 '20 at 13:06
  • `yarn rebuild` isn't a command (or at least it's not anymore). See: https://github.com/yarnpkg/yarn/issues/2069 – Daniel Mar 04 '21 at 04:35
16

You are missing dependencies:

npm install --save-dev prettier husky lint-staged
Mindaugas Jaraminas
  • 3,261
  • 2
  • 24
  • 37
  • 1
    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. ! – Rupesh Kumar Tiwari Aug 29 '18 at 20:25
13

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

Geoff Davids
  • 887
  • 12
  • 15
10

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
Black Mamba
  • 13,632
  • 6
  • 82
  • 105
8

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",
...
W.Perrin
  • 4,217
  • 32
  • 31
5

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.

Frondor
  • 3,466
  • 33
  • 45
3

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
Fatih Bulut
  • 2,385
  • 1
  • 15
  • 12
3

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

chmod +x .husky/pre-commit
Tamlyn
  • 22,122
  • 12
  • 111
  • 127
2

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.

squillman
  • 13,363
  • 3
  • 41
  • 60
2

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

Ayush Shankar
  • 307
  • 2
  • 9
2

Thanks. this one work for me

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" 
cng.buff
  • 405
  • 4
  • 5
1

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.

Aanchal1103
  • 917
  • 8
  • 21
1

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

Luis Febro
  • 1,733
  • 1
  • 16
  • 21
1

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
Phil Gibbins
  • 492
  • 5
  • 13
1

If you are using the latest husky version, make sure to install the hook after you add it. In husky 4, this was done automatically.

npx husky add .husky/pre-commit "lint-staged; git add ."
husky install .husky

This solved issue for me.

aumkar
  • 11
  • 2
0

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"
  ]
madhu131313
  • 7,003
  • 7
  • 40
  • 53
0

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

d_bhatnagar
  • 1,419
  • 1
  • 12
  • 20
0

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
Aravind Siruvuru
  • 1,019
  • 12
  • 7
0

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"
  ]
}
0

What worked for us: (for some reason this was not mentioned in other solutions)

npx husky install

Situation:

Husky pre-commit hook was configured and worked for some time. We missed the exact moment then it stopped triggering, but supposedly it is related to one of Husky's updates.

Running command

git config core.hooksPath

returned nothing.

After running the command mentioned above, the same git config command returned

.husky

Husky version 8.0.3, hope this helps anybody!

0

It could be that lint-staged is actually running but your files have no lint errors at all. If your files only have warnings, the pre-commit hook will not do anything and lets you commit. So make sure you actually have lint errors.

I know it's not exactly an answer to the question, but there are already lots of great answers and this can also be a cause that can be easily overlooked. (Heard it from a friend )

Thomas Soos
  • 150
  • 2
  • 11
0

my problem was that i forgot to add pre-commit file in the .husky directory (.husky/pre-commit)

its content might be something like this:

#!/bin/sh

. "$(dirname "$0")/_/husky.sh"

# Run lint-staged
npx lint-staged
moeinghasemi
  • 81
  • 1
  • 10
-1

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"
  ]
}
...
Gilson Viana
  • 723
  • 1
  • 8
  • 13
-1

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.

Edward_GM
  • 1
  • 1