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
lint-staged not running on precommit
提问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 中设置了husky和lint-staged配置。它不会将其添加为依赖项或安装它们。
The solution for me was:
我的解决方案是:
npm i -D husky lint-staged
npx mrm lint-staged
npm i -D husky lint-staged
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_modules
folder and rerun npm install
如果它对其他人有帮助:另一件要尝试的事情是删除您的node_modules
文件夹并重新运行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.
我最初npm install
在 Windows 10 机器上的 linux 子系统中运行。通过 bash 使用 git 一切正常。在 Powershell 中切换到 git 后,我收到了错误消息。卸载并重新安装 prettier、husky 和 lint-staged 对我不起作用。
I deleted my node_modules folder and reran npm install
from the Windows side and now it works.
我删除了我的 node_modules 文件夹并npm install
从 Windows 端重新运行,现在它可以工作了。
回答by Aanchal1103
回答by madhu131313
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
对象内部添加了对象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-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.
顺便说一下,安装后husky
,只需检查.git/hooks/pre-commit
内容。如果其中没有husky
类似的词,只需删除.git/hooks/pre-commit
文件并重新安装husky
或运行npx husky
. 因为如果文件不一样或不一样,husky
将跳过修改.git/hooks/pre-commit
文件。GHook
PreCommit
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