githuskygit-husky

when my mac commits code using Git, rejected by Husky, prompt ` sh:command not found `


When my Mac commits code using Git, it gets rejected by Husky, and I see the following error:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
.husky/_/husky.sh: line 23: sh: command not found
husky - pre-commit hook exited with code 127 (error)

The Terminal or VSCode also report mistakes.

How can I get past this error?


Solution

  • This is about github.com/typicode/husky/husky.sh#L23

    sh -e "$0" "$@"
    

    Make sure sh is in your $PATH (typically /bin/sh)

    Add an echo "prg='$0'" to your .husky/_/husky.sh just to debug and check if it is '$0' which is not found (being empty)

    And check, for PATH problem, the issue 912

    I use VS Code and Husky v6.

    I ran echo $PATH from the root of my project.
    Then I copied the output to the pre-commit file in the .husky directory.

    My pre-commit file looks like this:

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    npx lint-staged
    

    As mentioned by the OP, refering to Husky/ Command not found, the ~/.huskyrc should set NVM properly:

    # ~/.huskyrc
    # This loads nvm.sh and sets the correct PATH before running hook
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"