You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用husky v8提交代码时遇pre-commit钩子127错误求助

Husky v8 提交时npx: command not found 解决方法
  • 方法1:用npm exec替代npx
    把pre-commit里的npx lint-staged替换成npm exec lint-staged,npm exec是npm内置命令,不需要依赖npx的系统路径,兼容性更强。修改后的文件内容:

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    npm exec lint-staged
    
  • 方法2:手动添加Node.js的PATH到hook中
    先在正常终端里执行which node,找到Node.js的安装目录(比如/usr/local/bin),然后在pre-commit里添加环境变量:

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    # 替换成你实际的Node.js bin目录
    export PATH="/usr/local/bin:$PATH"
    npx lint-staged
    
  • 方法3:让Git使用你常用的终端shell
    如果是macOS用户,Git默认可能用系统shell,和你平时用的zsh/bash环境变量不一致。执行以下命令设置Git使用你常用的shell:

    git config --global core.shell /bin/zsh
    

    把/bin/zsh换成你实际使用的shell路径即可,这样Git执行hook时会加载你shell的完整环境变量。

  • 方法4:直接调用本地node_modules里的lint-staged
    跳过npx,直接调用项目本地安装的lint-staged:

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    ./node_modules/.bin/lint-staged
    

    这种方式完全不依赖全局环境,最稳定可靠。

内容的提问来源于stack exchange,提问作者Yaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 23:30:54