git lint-staged 未在预提交时运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50048717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:06:24  来源:igfitidea点击:

lint-staged not running on precommit

javascriptgitpre-commit-hookprettierlint-staged

提问by Andrew Horn

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:

这是我的 package.json 文件的相关部分:

"scripts": {
    "precommit": "lint-staged"
  },
"lint-staged": {
  "*.{js,json,css,scss,html,md}": [
    "prettier --write",
    "git add"
  ]
},

Edit. Here are the relevant devDependencies:

编辑。以下是相关的 devDependencies:

"devDependencies": {
  "husky": "^0.14.3",
  "lint-staged": "^7.0.4",
  "prettier": "1.12.0"
},

采纳答案by Andrew Horn

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

重新安装了 husky,现在似乎可以正常工作了。感谢@mpasko256 的帮助!

回答by Juanma Menendez

The problem for me was I ran "npx mrm lint-staged" like the official website says but it only set the huskyand lint-stagedconfigurations in package.json. It does not add then as dependency or installed them.

对我来说,问题是我像官方网站所说的那样运行了“ npx mrm lint-staged”,但它只在 package.json 中设置了huskylint-staged配置。它不会将其添加为依赖项或安装它们。

The solution for me was:

我的解决方案是:

  1. npm i -D husky lint-staged

  2. npx mrm lint-staged

  1. npm i -D husky lint-staged

  2. npx mrm lint-staged

回答by Mindaugas Jaraminas

You are missing dependencies:

您缺少依赖项:

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

回答by Danna

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

在您配置此脚本之前,您的 husky 包可能已经在您的 node_modules 中。尝试重新安装钩子,你可以运行

npm rebuild

or

或者

yarn rebuild

It solved my problem.

它解决了我的问题。

回答by squillman

In case it helps someone else: another thing to try is to delete your node_modulesfolder and rerun npm install

如果它对其他人有帮助:另一件要尝试的事情是删除您的node_modules文件夹并重新运行npm install

I originally ran npm installin 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.

我最初npm install在 Windows 10 机器上的 linux 子系统中运行。通过 bash 使用 git 一切正常。在 Powershell 中切换到 git 后,我​​收到了错误消息。卸载并重新安装 prettier、husky 和 ​​lint-staged 对我不起作用。

I deleted my node_modules folder and reran npm installfrom the Windows side and now it works.

我删除了我的 node_modules 文件夹并npm install从 Windows 端重新运行,现在它可以工作了。

回答by Aanchal1103

The problem in my case was that there were some existing hooks and huskydoes not override them (more info here).

我的问题是有一些现有的钩子并且husky没有覆盖它们(更多信息在这里)。

Just putting it here in case someone else runs into the same issue.

只是把它放在这里以防其他人遇到同样的问题。

回答by madhu131313

I had the same problem, but I did this mistake.

我有同样的问题,但我犯了这个错误。

I have added lint-stagedobject inside huskyobject, but later realized I need to add lint-stagedkey-value pairs as direct key-value pairs in package.json

我在lint-staged对象内部添加了对象husky,但后来意识到我需要添加lint-staged键值对作为直接键值对package.json

"lint-staged": {
  "*.{js,json,css,scss,html,md}": [
    "prettier --write",
    "git add"
  ]

回答by W.Perrin

I think there was something wrong with your package.json.

我认为您的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-commitcontent. If no huskylike word in it, just remove the .git/hooks/pre-commitfile and reinstall huskyor run npx husky. Because huskywill skip modifying the .git/hooks/pre-commitfile if it is not GHookalike or PreCommitalike.

顺便说一下,安装后husky,只需检查.git/hooks/pre-commit内容。如果其中没有husky类似的词,只需删除.git/hooks/pre-commit文件并重新安装husky或运行npx husky. 因为如果文件不一样或不一样,husky将跳过修改.git/hooks/pre-commit文件。GHookPreCommit

You may find it out by following this link. https://github.com/typicode/husky/blob/master/src/installer/hooks.ts#L58

你可以通过这个链接找到它。 https://github.com/typicode/husky/blob/master/src/installer/hooks.ts#L58

One alternative is to use pre-commit.

一种替代方法是使用pre-commit.

yarn add --dev pre-commit
"scripts":{
   ...
},
"pre-commit":"lint-staged",
...

回答by d_bhatnagar

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

请注意您使用的节点版本。Husky 需要 node >= 10 并且 lint-staged 需要 node >= 10.13