bashgitnpmhusky

Why is Husky pre-commit hook failing on Windows?


On Windows, Husky pre-commit hooks fail with the following error message:

/usr/bin/env: 'bash': No such file or directory
husky - pre-commit hook exited with code 127 (error)
husky - command not found in PATH=/mingw64/libexec/git-core:/mingw64/bin:/usr/bin:/cmd:/mingw64/bin:/usr/bin

However my commit hook only runs a npm command that does not involve bash. Why is this command looking for bash and failing to do so?


Solution

  • Husky hooks are executed with sh rather than cmd, even on Windows. If the hook contains a npm command, it will try to execute npm.sh (e.g. C:\Program Files\nodejs\npm) rather than npm.cmd from the same folder.

    This C:\Program Files\nodejs\npm script relies on bash, as shown in the shebang line:

    #!/usr/bin/env bash
    

    If you are using a stripped down installation of Git that does not include bash such as MinGit (widely used in CI), you might get the error above. In this case, you might want to use the complete Git for Windows to include bash. Else you need to modify your Git hooks to be Windows-compliant.