How to fix 'Git: .husky/pre-commit: 4: npm: not found' error

The surprise

What sorcery is this? I'm just trying to commit code.

It's Husky, not this Husky

What again!

Basically, the Husky configuration can't find the npm executable to run the scripts' commands. it's a path conflict thingy; to check the location of npm, run which npm

The fix

Terminal

For Windows, you can use WSL or try the Git Bash CLI.

touch .huskyrc && echo 'export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' > .huskyrc

GUI

Open your file explorer, create a .huskyrc file in your home directory, and input:

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

I hope this fixes your Husky problem.

Thanks to Hossein Mousavi on Stack Overflow.