husky icon indicating copy to clipboard operation
husky copied to clipboard

[Windows] [lint-staged] [GitHub Desktop] .husky/pre-commit script is unable to find `npx`

Open trusktr opened this issue 1 year ago • 2 comments

Troubleshoot

  • [x] Before creating an issue, please check: https://typicode.github.io/husky/troubleshooting.html

Context

Please describe your issue and provide some context:

  • Terminal or GUI client (PowerShell, Git Bash, GitHub Desktop, ...) GitHub Desktop
  • If applicable, content of the failing hook:
    #!/usr/bin/env sh
    . "$(dirname -- "$0")/_/husky.sh"
    
    npx lint-staged
    
  • If possible, minimal steps to reproduce the issue
    • There are no minimal steps for Windows (f.e. install Node.js, try a lot of things, hope it works, end with a workaround, then don't go back and figure it all out again).

Error:

/bin/bash: D:/Program Files (x86)/nodejs/npx: No such file or directory
husky - pre-commit hook exited with code 127 (error)
husky - command not found in PATH=...

However I can git commit just fine in PowerShell or CMD, and it runs the hooks. I can see npx and other commands in my Path.

I get this issue when trying to commit in GitHub Desktop.

Workaround:

Using the direct path to the executable without npx solved the issue for now:

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

./node_modules/.bin/lint-staged

Thank you!

trusktr avatar Aug 02 '23 22:08 trusktr

It may be a little strange workaround, but in the end we have to run it via a package manager only because node_modules/.bin is not added to the path so husky cannot find the relevant commands.

If you were to manually modify the path it would work just fine without it.

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

export PATH="$PATH:./node_modules/.bin"
lint-staged --debug

Works with nested projects as well, just modify the path to node_modules since it's run from the top level directory.

I prefer this method over yours, since lint-staged might run other scripts that will also be unrecognized by husky otherwise.

KonkretneKosteczki avatar Oct 27 '23 07:10 KonkretneKosteczki

Is there any solution to this problem? Please help me (without changing the PATH)

timi137137 avatar Nov 15 '23 09:11 timi137137