我正在使用 Husky (7.0.0) 在现有电子项目(在 Windows 上)上设置预提交 Hook 。为了安装 Husky,我按照文档 ( here ) 操作,并且使用两个文件正确创建了 .husky 文件夹: 哈士奇.sh
#!/bin/sh
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}
readonly hook_name="$(basename "$0")"
debug "starting $hook_name..."
if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi
if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi
export readonly husky_skip_init=1
sh -e "$0" "$@"
exitCode="$?"
if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi
exit $exitCode
fi
和预提交:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm test
所以现在在提交之前应该运行测试脚本(测试脚本仅包含运行 jest,即“test”:“jest”)。但是当我运行 git commit (从终端)时,它失败并显示以下输出: console output 。 错误消息指出 git 无法找到 Node ,但看起来真正奇怪的是输出的前两行,这表明 jest 实际运行。我正在使用 Node 18.0.0 (通过 nvm-windows 管理)和 npm 7.24.2 (这两个在 windows terminal 和 git bash 中均得到确认)。 Node 的路径已经在我的 $PATH 环境变量中。 如果我可以添加任何详细信息,请告诉我
最佳答案
我发现了我的问题,我的环境变量列表中有一个双引号,它使以下所有路径无效(其中有nodejs路径)。为了理解这一点,我通过 git bash 手动运行命令并列出 PATH 环境变量。无论如何,仍然有一些我无法理解的事情,在 git bash 中,如果我运行 npm -v
它会显示正确的 Node 版本,但尝试运行其他命令会导致错误
关于javascript - Windows 上的 Git Hook 无法识别 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71950446/