lint-staged没有在precommit上运行

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和我展示huskylint-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)

  • 正如 @Geoff 在下面的答案中提到的,钩子不会使用 `husky@4` 自动安装,预提交钩子可以工作。这应该是当前接受的答案 (3认同)
  • 对于“npm set-script prepare”,我得到“sh:husky:找不到命令”——您是否将其安装为全局? (2认同)

And*_*orn 22

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

  • 我认为这个技巧现在不起作用,我已经通过清除缓存来完成此操作,但没有起作用 (3认同)

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

我在这里尝试了很多解决方案,但最终组合成功了!

  1. 确保安装了 Husky v4。v6 从来没有对我触发过。
  2. 检查输出git config core.hooksPath。这应该返回任何东西。如果它运行,
git config --unset core.hookspath
Run Code Online (Sandbox Code Playgroud)

最后它奏效了!

  • 我简直不敢相信,效果这么好,非常感谢我的哈士奇 3.0.4 (3认同)
  • 它真的有效..有什么解释为什么它有效吗? (3认同)
  • 是的,这有效。我已经从 husky 5 降级到 4。但是 v4 不起作用 - 尽管相同的设置在另一个项目中起作用。该解决方案是缺少的步骤。 (2认同)
  • 谢谢@JoãoIgnacio——但我不是男人。:') (2认同)
  • 很抱歉@SwaathiKakarla!谢谢你,女士! (2认同)
  • 现在这对我来说甚至可以与 husky@7.0.4 一起使用!好的 (2认同)

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)


Geo*_*ids 9

对于遇到此问题并使用 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


Min*_*nas 8

您缺少依赖项:

npm install --save-dev prettier husky lint-staged
Run Code Online (Sandbox Code Playgroud)


W.P*_*rin 6

我认为您的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)


Tam*_*lyn 6

对我来说,问题是该pre-commit钩子不可执行,这很容易修复:

chmod +x .husky/pre-commit
Run Code Online (Sandbox Code Playgroud)


Jua*_*dez 5

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

我的解决方案是:

  1. npm i -D哈士奇绒

  2. npx mrm皮棉分段

  • 拯救了我的一天!运行“npx mrm lint-staged”,然后钩子就像一个魅力一样工作。 (2认同)

Fro*_*dor 5

这发生在我身上,这些答案都没有帮助。所以为了将来参考,这是因为我正在使用npm@7which看起来它不知道如何正确执行 husky

我发现这是 husky 和 ​​npm 的问题的方式是因为我发现目录中没有pre-commit文件my-project/.git/hooks

当您安装 husky 时,它会自动在此类文件夹中为您施展魔法。所以为此,我不得不:

  1. 降级到 npm i -g npm@6
  2. 确保所有内容都重新安装rm -rf node_modules package-lock.json && npm i(您应该在控制台中看到 Husky 输出)
  3. 虽然它不是真的需要,但我再次执行 npx mrm lint-staged

最后,它奏效了。