TER*_*TER 9 macos githooks git-commit atlassian-sourcetree husky
当我在节点项目中使用 husky hooks 并尝试通过应用程序 SourceTree 提交到 git 时,我看到错误。挂钩在package.json中指定。我正在使用 nvm 来管理节点版本。
Can't find node in PATH husky > pre-commit hook failed
使用SourceTree进行git提交时,会报错。
一旦绕过这个错误,当使用 SourceTree 的嵌入式 git 通过 SourceTree 尝试提交时,husky 会报告另一个错误:“当前目录不是 git 目录!”。
我该如何解决这些错误?
TER*_*TER 22
(1) 解决第一个错误“Can't find node in PATH”:
由于您是从应用程序(SourceTree)而不是从命令行运行 git,因此 husky 没有环境变量来查找节点。为了解决这个问题,husky提供了一种机制:它会在运行hooks之前运行用户指定的文件 ~/.huskyrc。
这对我有用:
在.bashrc中,我在设置 PATH 后添加了这一行:
echo "export PATH="$(dirname $(which node)):$PATH"" > ~/.huskyrc
Run Code Online (Sandbox Code Playgroud)
husky 文档中描述了另一个建议, https://typicode.github.io/husky/#/ ?id=command-not-found
# ~/.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"
Run Code Online (Sandbox Code Playgroud)
(2)解决“当前目录不是git目录!” 许多用户报告说,更改 git 版本已经解决了这个问题。
如果您使用 SourceTree,则可以将其配置为使用 SourceTree 内的嵌入式 git 版本或系统版本。当我从 SourceTree 中的嵌入式版本(2.31.0)切换到系统版本(我的系统上的 2.30.1)时,错误得到解决。
在 SourceTree 中,进入Preferences -> git设置 git 版本。
使用 Husky 和 SourceTree 最通用和正确的方法将是下一个~/.huskyrc
#!/usr/bin/env bash
# Load NVM support and all defined ENV variables
# (use only one string from below depends on which shell you use)
source ~/.zprofile #macOS 12+ with ZSH
source ~/.bash_profile #for older macOS 11
# Check if this repo has .nvmrc file and use it to set proper Node.js version
if test -f ".nvmrc"; then
nvm use
fi
Run Code Online (Sandbox Code Playgroud)
如果不是.zprofile,或者.bash_profile在您的情况下,请使用另一个文件,它应该包含nvm.shNVM 所需的所有设置脚本。在某些情况下,第一步可能是可选的,具体取决于 bash 配置文件设置。
| 归档时间: |
|
| 查看次数: |
14170 次 |
| 最近记录: |