【问题标题】:Husky v5 doesn't create Git hooksHusky v5 不创建 Git 钩子
【发布时间】:2021-03-12 09:40:42
【问题描述】:

我已经在我的应用程序中安装了 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 $0)/_/husky.sh"

lint-staged

package.json

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

【问题讨论】:

    标签: git husky


    【解决方案1】:

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

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

    在完成我的设置时,我从不同的位置运行并重新运行了几次安装。我发现 this list 的指令有助于确保我每次都将我的 git 配置重置为一致的状态,尤其是提到 hooksPath 的那一行。

    【讨论】:

      【解决方案2】:

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

      #!/bin/sh
      [ -z "$CI" ] && exit 0
      
      . "$(dirname $0)/_/husky.sh"
      
      yarn lint-staged
      

      那是因为:

      如果您直接调用本地安装的二进制文件,则需要 通过您的包管理器运行它们

      您可以找到herehere 的更多信息。希望有帮助?

      【讨论】:

        【解决方案3】:

        husky v5 不生成钩子(不能说为什么)
        所以我降级到 4.3.8 并删除了 .git/hooks(不必要):

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

        【讨论】:

        • 可以确认。使用husky@5.0.9,未创建 git hooks 文件夹,并且在提交时抱怨找不到husky-run。降级到husky@4.3.8 工作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-11
        • 2020-08-12
        • 1970-01-01
        • 2020-10-09
        • 2012-02-24
        • 1970-01-01
        • 2011-05-17
        相关资源
        最近更新 更多