Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

husky - pre-commit hook exited with code 3 (error) #1176

Closed
cloecloe opened this issue Jul 25, 2022 · 5 comments
Closed

husky - pre-commit hook exited with code 3 (error) #1176

cloecloe opened this issue Jul 25, 2022 · 5 comments

Comments

@cloecloe
Copy link

cloecloe commented Jul 25, 2022

Hello,

I have an issue with husky and the pre-commits on a project. I have husky set up on other projects and it is working. On this specific project I can not commit anymore but my colleagues can. No matter the commit, even empty, I have this error:husky - pre-commit hook exited with code 3 (error)
I have tried a bunch of things, and deleting the node_modules, husky.sh and .git/hooks and trying to find information on the error code 3 online but could not find anything. Would you know what code 3 matches to please?

I have the version 8.0.1 in a node project, using yarn.

The pre-commit file:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

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

npx lint-staged

In the package.json I have the ling-staged:
"lint-staged": { "**/*": "yarn prettier --write --ignore-unknown" }

The husky.sh file:
#!/usr/bin/env sh
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}

readonly hook_name="$(basename -- "$0")"
debug "starting $hook_name..."

if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi

if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi

readonly husky_skip_init=1
export husky_skip_init
sh -e "$0" "$@"
exitCode="$?"

if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi

if [ $exitCode = 127 ]; then
echo "husky - command not found in PATH=$PATH"
fi

exit $exitCode
fi

Thanks for your help!

@doctone
Copy link

doctone commented Aug 15, 2022

Make sure you have permissions in all your folders / files from administrator profile- this solved it for me

@cloudyan
Copy link

我也遇到了,修改项目文件操作权限并不解决这个问题

@doublethefish
Copy link

Try adding -x to the sh -e "$0" "$@" line. This will enable tracing mode.

So make it look like: sh -x -e "$0" "$@"

@farlandlee
Copy link

farlandlee commented Aug 31, 2022

we had this issue on one of my colleagues and it turned out it was a minor version difference for node. Specifically he was running node 16.17 and the .nvmrc file for the project specified 16.13. So we installed 16.13 on his computer, changed to using that version and problem solved!

Specific Steps:

  1. go to the root of your project and check the .nvmrc file for the version of Node that the project requires.
  2. Install that version using nvm install vXX.XX (the minor version must match)
  3. That should automatically set that version of node to active, but just to be sure, use nvm use XX.XX for the correct minor version.
  4. Run your commit again.

Hopefully that will solve it! Step 3 may not even be necessary as it should automatically set the version of node to the version shown in .nvmrc while husky is running once that minor version is, in fact, installed on your machine. On my computer I have node 18.7.0 running and I had no issues since I did have 16.13 installed but not active.

@cloecloe
Copy link
Author

cloecloe commented Nov 1, 2022

Hi, thanks all for your suggestions. What ended up working for me was to remove 2 lines from my pre-commit file:

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

@cloecloe cloecloe closed this as completed Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants