提交新代码时如何修复错误'not found husky-run'?

How to fix error 'not found husky-run' when committing new code?

在提交使用 Husky 的项目时,我收到一条错误消息 not found husky-run

我检查了 package.json 并且它有 husky 作为依赖项,我可以在 package.json 中看到 Husky 的预提交钩子配置。所以我不知道该怎么做才能解决这个问题。此外,我团队中的其他成员可以提交,而 husky 会为他们工作。

我也试过rm -rf node_modules && npm install然后再次提交,但仍然出现同样的错误。

还有其他人知道如何解决这个问题吗?

要解决此问题,有两种方法,具体取决于您已经使用的 Husky 版本。

如果您使用的是 Husky v4 或更低版本,请执行以下操作:

rm -rf .git/hooks
npm install

对于 Husky v7 或更高版本,请执行以下操作:

# For NPM
 npm install husky@7 --save-dev \
      && npx husky-init \
      && npm exec -- github:typicode/husky-4-to-7 --remove-v4-config

# For Yarn
 yarn add husky@7 --dev \
  && npx husky-init \
  && npm exec -- github:typicode/husky-4-to-7 --remove-v4-config
# or
 yarn add husky@7 --dev \
  && yarn dlx husky-init --yarn2 \
  && npm exec -- github:typicode/husky-4-to-7 --remove-v4-config

此时您应该能够提交并让您的挂钩再次工作。

如有任何问题,请阅读documentation for migration from 4 to 7

不要删除 .get/hooks 挂钩将不起作用。 根据 migrating manual from 4 to 6 version :

对于 npm 用法执行

 npm install husky@6 --save-dev \
      && npx husky-init \
      && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config

对于纱线的使用:

 yarn add husky@6 --dev \
  && npx husky-init \
  && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config

yarn add husky@6 --dev \
  && yarn dlx husky-init --yarn2 \
  && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config

如果在此过程中出现任何错误,您可以通过执行以下操作简单地还原更改:

rm -rf .husky && git config --unset core.hooksPath

解释发生了什么:

husky init 设置 Git 挂钩并更新您的 package.json 脚本(您可能希望在 运行 husky init 之前提交对 package.json 的更改)。

husky-4-to-6 根据您的 husky v4 配置创建挂钩。如果传--remove-v4-config,之前的配置会被删除(推荐)。

要在 husky 版本 6 中修复此问题 运行:

yarn husky install

只需一个“纱线安装”就为我解决了这个问题

只有这个 'yarn add husky@6 --dev' 在你的终端里

我只需要在 package.json:

中添加一个准备脚本
"scripts": {
  ...
  "prepare": "husky install",
  ...
}

然后运行yarn install或者npm install和husky会被初始化。这将确保查看您的回购协议的人也能够 运行 哈士奇。

这对我有用:

如果您还没有文件 ~/.huskyrc,请添加一个文件

  • 触摸 ~/.huskyrc
  • 打开~/.huskyrc
  • 粘贴以下内容:
# ~/.huskyrc
# This loads nvm.sh and sets the correct PATH before running hook
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"