Husky v5 不会创建 Git 个挂钩

Husky v5 doesn't create Git hooks

我已经在我的应用程序中安装了 Husky v5,我想在提交时 运行 lint-staged 命令。

我已经按照 Getting Started 文档进行操作,但在我的 git 配置文件中没有创建 .git/hooks/pre-commit 文件。

所以,当我提交时,挂钩不是 运行 并且提交会直接通过而不会被 lint-staged 检查。

我尝试了 运行宁 yarn add -D husky@nextnpm i -D husky@next。 我还尝试删除 node_modules 和 npm rebuild.

.husky/pre-commit

#!/bin/sh
[ -z "$CI" ] && exit 0

. "$(dirname [=11=])/_/husky.sh"

lint-staged

package.json

"scripts": {
  "postinstall": "husky install"
},

有点晚了,但我今天也遇到了这个问题。经过大量搜索,我发现 this issue 描述了涉及 Yarn 的安装问题。在我的情况下,yarn 不正确 运行 来自 husky 的 post-install 脚本,并且按照那个 thead 上的建议,我发现将 package.json 中的 postinstall 行更改为此解决了我的问题:

{
    "postinstall": "node ./node_modules/husky/lib/installer/bin install"
}

在完成设置时,我从不同的位置运行 和重新运行 安装了好几次。我发现 this list 的说明有助于确保我每次都将 git 配置重置为一致的状态,特别是提到 hooksPath.

的行

您需要在 .husky/pre-commit 文件中的 lint-staged 之前添加 yarn

#!/bin/sh
[ -z "$CI" ] && exit 0

. "$(dirname [=10=])/_/husky.sh"

yarn lint-staged

那是因为:

If you were calling directly locally installed binaries, you need to run them via your package manager

您可以找到更多信息 here and here。希望有帮助

husky v5不生成hooks(说不出原因)
所以我降级到 4.3.8 并删除了 .git/hooks(不需要):

rm -rf .git/hooks
yarn add -D husky@4.3.8