Committing fails in Git hooks pre-commit because the node command wasn't found
Asked Answered
R

8

22

I generated a JHipster app with Angular and Java, inside of a repository that I had previously made. I then generated some JDL classes with it and the build was successful, but when I tried to commit my changes in GitHub, it threw the following error:

Commit failed - exit code 1 received, with output: '.git/hooks/pre-commit: line 32: node: command not found'

I looked inside of my pre-commit file:

#!/bin/sh
# husky

# Hook created by Husky
#   Version: 1.3.1
#   At: 2/13/2019, 12:10:11 PM
#   See: https://github.com/typicode/husky#readme

# From npm package
#   Name: husky
#   Directory: undefined
#   Homepage: https://github.com/typicode/husky#readme

scriptPath="JHipsterProject/node_modules/husky/run.js"
hookName=`basename "$0"`
gitParams="$*"

debug() {
  [ "${HUSKY_DEBUG}" = "true" ] && echo "husky:debug $1"
}

debug "$hookName hook started..."

if [ -f "$scriptPath" ]; then
  # if [ -t 1 ]; then
  #   exec < /dev/tty
  # fi
  if [ -f ~/.huskyrc ]; then
    debug "source ~/.huskyrc"
    source ~/.huskyrc
  fi
  node "$scriptPath" $hookName "$gitParams"
else
  echo "Can't find Husky, skipping $hookName hook"
  echo "You can reinstall it using 'npm install husky --save-dev' or delete this hook"
fi

The error was in line 32:

node "$scriptPath" $hookName "$gitParams"

I'm not familiar with pre-commit files or how they work, but I currently have v10.15.0for Node.js, and 1.8.0_201 for my Java JDK and JRE. The version of JHipster I'm using is 5.8.1.

Is there anything I should change in this file, including line 32 in order to get rid of the error in my commit?

I'm also using the Visual Studio Code IDE if that helps at all.

Thanks in advance.

Rhianna answered 13/2, 2019 at 20:23 Comment(1)
The node version you have should run fine. Can you try running npm install again as this trigger the hook install script run.Stlaurent
W
19

As @Stephen Savitzky suggested, it might be Node installation problem. However, if you're able to

  1. Run application normally without an issue, and also
  2. See no issues when doing git commits from terminal

Then, it's probably Node sourcing problem since the paths to it might be different from terminals or from GUI apps like VSC.

Your setup seems to be using husky for pre-commit hooks, so to ensure you have the right Node version, you could add ~/.huskyrc as suggested in the docs:

# ~/.huskyrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Then, you can source Node from NVM (if you use one) or another source. It's also a good way to debug what's actually going on when husky hook scripts kick in.

Whoosh answered 24/10, 2019 at 10:43 Comment(1)
I installed asdf and nuked my nvm today. I found out I just needed to restart VS Code :)Gyral
J
5

"node: command not found" means that there is no program called node on any of the directories in $PATH, the environment variable that tells the shell where to look for programs. Hooks are usually run with a very restricted $PATH; e.g. /bin:/usr/bin.

The best way to deal with this is to use an absolute path for any programs that aren't installed in either /bin or /usr/bin. You can find out what path to use with the which command:

> which node
/home/steve/.nvm/versions/node/v10.6.0/bin/node

Of course, it's also possible that node isn't installed at all on the machine the hook is running on.

Jemine answered 14/2, 2019 at 15:40 Comment(0)
C
0

You can add new entry to the Windows PATH environment variable using the Command Prompt as follows:

SET PATH=C:\Program Files\Nodejs;%PATH%

npm

Or you can set the PATH variable by using Windows graphical UI. (Annoying for me :/ )

Corry answered 2/2, 2022 at 8:58 Comment(0)
M
0

if you are using nvm there is the fix

#!/bin/bash
. $HOME/.nvm/nvm.sh
./node_modules/pre-commit/hook
RESULT=$?
[ $RESULT -ne 0 ] && exit 1
exit 0

Ref: https://github.com/observing/pre-commit/issues/139#issuecomment-437138661

Miser answered 8/4, 2022 at 11:14 Comment(0)
T
0

For me, I use bash as default shell and my ~/.bashrc has got a whole bunch of paths set, whereas my .git/hooks/pre-commit was as follows:

#!/bin/sh
exec mvn spotless:check

Changed #!/bin/sh -> #!/bin/bash and ta-da! all works

Tumpline answered 14/9, 2022 at 14:1 Comment(0)
M
0

Installing node js resolved this issue for me.

Multivibrator answered 13/7, 2023 at 6:20 Comment(0)
S
0

Assuming you're on some Unix system like Linux or macOS:

If you installed Node correctly, you'll likely have some sort of Node setup lines in your shell configuration. Something like ~/.bash*, ~/.z* or whatever else depending on which shell you use.

If you have these in your ~/.bashrc (or ~/.zshrc, etc.), then they will execute only in interactive shells. Make sure to move them to your ~/.bash_profile (or ~/.zprofile, etc.).

Sanctity answered 30/1, 2024 at 1:52 Comment(0)
C
-1

Not sure how Husky works but according to the limited (and maybe inaccurate) knowledge I have about Husky, it executes when some git actions are called and allows you to interact with git hooks. But getting straight to the point, you can apparently "bypass" it by using --no-verify flag.

So try using git commit -m "commit message" --no-verify

Sorry for the almost senseless explanation but this worked for me. I hope it helps.

Clarita answered 16/3, 2023 at 5:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.