Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
807 views
in Technique[技术] by (71.8m points)

javascript - 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"
},
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...