Node not reconized with Git hook on windows

1.3k Views Asked by At

I am setting up a precommit hook on an existing electron project (on Windows) using Husky (7.0.0). To install Husky I followed the documentation (here) and the .husky folder was correctly created with the two files: 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

and pre-commit:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm test

So now before commiting the test script should be run (the test script consists just in running jest, i.e. "test":"jest"). But when I run git commit (from terminal) it fails with this output: console output. The error message states that git is not able to find node, but what it looks really strange are the first two lines of the ouput, which are suggesting that jest actually run. I am using node 18.0.0 (managed via nvm-windows) and npm 7.24.2 (these two are confirmed both in windows terminal and in git bash). The path to node is already in my $PATH environment variable. Please let me know if I can add any detail

2

There are 2 best solutions below

0
On BEST ANSWER

I found my problem, I had a double quotes in my environment variables list that was invalidating all the following paths (among which there was the nodejs path). To understand this thing I manually ran the commands thorugh the git bash and listed the PATH env variable. Anyway there is still something that I am not able to understand, in the git bash if i ran npm -v it showed the correct node version, but trying to run other commands caused error

3
On

That was reported before (typicode/husky issue 980), referring to the "Command not found" documentation

If your ~/.huskyrc content is correct, the path should be set.

Check that be replacing, in your pre-commit, npm test by echo $Path (inspired by issue 462).