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"
},
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".
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.
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.
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.
'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.
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.
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
I tried so many solutions on here but a combination finally worked!
git config core.hooksPath
. This should not return anything. If it does run,git config --unset core.hookspath
And FINALLY it worked!
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
Reinstalled husky and now seems to be working. Thanks @mpasko256 for your help!
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
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.
You are missing dependencies:
npm install --save-dev prettier husky lint-staged
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.
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.
https://dev.to/typicode/what-s-new-in-husky-5-32g5
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
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",
...
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:
npm i -g npm@6
rm -rf node_modules package-lock.json && npm i
(you should see Husky output in the console)npx mrm lint-staged
Finally, it worked.
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
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.
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.
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
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
For me the problem was that the pre-commit
hook was not executable which was easily fixed:
chmod +x .husky/pre-commit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With