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