And*_*orn 13 javascript git pre-commit-hook prettier lint-staged
更漂亮的是没有预先运行.这在其他项目中使用相同的配置,所以我很困惑为什么这次不工作.
这是我的package.json文件的相关部分:
"scripts": {
"precommit": "lint-staged"
},
"lint-staged": {
"*.{js,json,css,scss,html,md}": [
"prettier --write",
"git add"
]
},
Run Code Online (Sandbox Code Playgroud)
编辑.以下是相关的devDependencies:
"devDependencies": {
"husky": "^0.14.3",
"lint-staged": "^7.0.4",
"prettier": "1.12.0"
},
Run Code Online (Sandbox Code Playgroud)
Nis*_*hah 40
2021年
有时不会添加钩子,husky因此您需要使用简单的 hack 来添加它。
在安装 husky 之后,您需要先卸载 husky,V4因为它确保您的钩子安装正确,然后安装最新版本,husky以便您获得最新更新。
新产品管理
npm uninstall husky
npm install -D husky@4
npm install -D husky
Run Code Online (Sandbox Code Playgroud)
纱
yarn remove husky
yarn add -D husky@4
yarn add -D husky
Run Code Online (Sandbox Code Playgroud)
如果有时上述伎俩没有工作,所以让我们添加勾成沙哑,下面提及的方法只用在V6和我展示husky用lint-staged的例子。
新产品管理
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
Run Code Online (Sandbox Code Playgroud)
纱
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
Run Code Online (Sandbox Code Playgroud)
Ank*_*aha 21
对我来说,问题是通过卸载和安装较低版本来解决的
npm uninstall husky
npm install -D husky@4 //after this it will work
Run Code Online (Sandbox Code Playgroud)
小智 19
node_modules在您配置此脚本之前,您的 husky 包可能已经在您的文件中。尝试重新安装钩子,您可以运行:
npm rebuild
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的是 yarn:
npm rebuild --update-binary
Run Code Online (Sandbox Code Playgroud)
它解决了我的问题。
Swa*_*rla 18
我在这里尝试了很多解决方案,但最终组合成功了!
git config core.hooksPath。这应该不返回任何东西。如果它运行,git config --unset core.hookspath
Run Code Online (Sandbox Code Playgroud)
最后它奏效了!
Bla*_*mba 12
找出原因并使用上述解决方案浪费了时间
阅读文档并避免谷歌搜索: https://typicode.github.io/husky/#/ ?id=automatic-recommended
或者按照以下步骤操作:
husky-init 是一个一次性命令,用于使用 husky 快速初始化项目。
npx husky-init && npm install # npm
npx husky-init && yarn # Yarn 1
yarn dlx husky-init --yarn2 && yarn # Yarn 2
Run Code Online (Sandbox Code Playgroud)
对于遇到此问题并使用 Husky 5 的任何人,不会自动安装挂钩。所以你可能根本没有所需的钩子.git/hooks folder。您需要将 a 添加postinstall到 package.json(推荐),或者npx husky install在 npm 安装包后运行。
或者只是降级到赫斯基 4。如果像我一样,您正在从事商业项目并且不想成为赫斯基赞助商,那么您实际上必须这样做。
https://dev.to/typicode/what-s-new-in-husky-5-32g5
我认为您的package.json.
"scripts":{
...
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "npm test"
}
},
"lint-staged": {
"*.ts": ["tslint", "prettier --write", "git add"]
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,安装后husky,只需检查.git/hooks/pre-commit内容。如果其中没有husky类似的词,只需删除.git/hooks/pre-commit文件并重新安装husky或运行npx husky. 因为如果文件不一样或不一样,husky将跳过修改.git/hooks/pre-commit文件。GHookPreCommit
你可以通过这个链接找到它。 https://github.com/typicode/husky/blob/master/src/installer/hooks.ts#L58
一种替代方法是使用pre-commit.
yarn add --dev pre-commit
Run Code Online (Sandbox Code Playgroud)
"scripts":{
...
},
"pre-commit":"lint-staged",
...
Run Code Online (Sandbox Code Playgroud)
对我来说,问题是该pre-commit钩子不可执行,这很容易修复:
chmod +x .husky/pre-commit
Run Code Online (Sandbox Code Playgroud)
对我来说,问题是我像官方网站上所说的那样运行了“ npx mrm lint-staged ”,但是它只在package.json中设置了沙哑的和lint-staged配置。然后,它不会添加为依赖项,也不会安装它们。
我的解决方案是:
npm i -D哈士奇绒
npx mrm皮棉分段
这发生在我身上,这些答案都没有帮助。所以为了将来参考,这是因为我正在使用npm@7which看起来它不知道如何正确执行 husky。
我发现这是 husky 和 npm 的问题的方式是因为我发现目录中没有pre-commit文件my-project/.git/hooks。
当您安装 husky 时,它会自动在此类文件夹中为您施展魔法。所以为此,我不得不:
npm i -g npm@6rm -rf node_modules package-lock.json && npm i(您应该在控制台中看到 Husky 输出)npx mrm lint-staged最后,它奏效了。
| 归档时间: |
|
| 查看次数: |
9515 次 |
| 最近记录: |