lint-staged not running on precommit

走远了吗. 提交于 2020-05-10 07:25:10

问题


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

回答1:


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




回答2:


You are missing dependencies:

npm install --save-dev prettier husky lint-staged



回答3:


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




回答4:


Probably your husky package already in your node_modules before you config this script. try to reinstall hooks, you can run

npm rebuild

or

yarn rebuild

It solved my problem.




回答5:


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.




回答6:


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.




回答7:


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



回答8:


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




回答9:


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


来源:https://stackoverflow.com/questions/50048717/lint-staged-not-running-on-precommit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!