0

环境

  • 节点已安装~/node-v14.15.3-linux-x64/bin/node并已设置在PATH变量中。
  • VSCode 1.55.2

问题

每次当我尝试提交代码中的更改时,我都会得到:

.husky/pre-commit: 4: npx: not found husky - pre-commit hook exited with code 127 (error)

代码

  • 哈士奇
#!/bin/sh
if [ -z "$husky_skip_init" ]; then
  debug () {
    [ "$HUSKY_DEBUG" = "1" ] && echo "husky (debug) - $1"
  }

  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)"
    exit $exitCode
  fi

  exit 0
fi

  • 预提交文件
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
npx lint-staged

我试过的

  • 添加"terminal.integrated.inheritEnv": false,这个settings.json
  • 像这样导出节点和npx .huskyrc
export NODEJS_HOME="$HOME/node-v14.15.3-linux-x64/bin"
export NPX_Dir="$HOME/node-v14.15.3-linux-x64/bin/npx"

但没有一个工作

请注意 ,当从 vscode 的终端使用 提交更改时git commit,预提交脚本会正常运行。

文档指出:

您可以在终端中回显 $PATH 并将您的应用配置为使用相同的值。


那么,我该怎么做呢?

4

1 回答 1

0

Experienced this problem recently. This error '.husky/pre-commit: 4: npx: not found husky - pre-commit hook exited with code 127 (error)' was because I had not installed node on my system. I suggest trying this https://www.newline.co/@Adele/how-to-install-nodejs-and-npm-on-macos--22782681

于 2021-12-07T01:02:27.780 回答