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

Can't find Husky, skipping pre-commit hook #333

Closed
DarwinniwraD opened this issue Aug 21, 2018 · 29 comments
Closed

Can't find Husky, skipping pre-commit hook #333

DarwinniwraD opened this issue Aug 21, 2018 · 29 comments

Comments

@DarwinniwraD
Copy link

yarn in v1.6.0

husky installed information:

"husky": "^0.14.3"

config in .huskyrc

{
  "hooks": {
    "pre-commit": "npm run lint"
  }
}

I got the below error message:

Can't find Husky, skipping pre-commit hook
You can reinstall it using 'npm install husky --save-dev' or delete this hook
@alexeyten
Copy link
Contributor

Have you tried to do what error message suggests?

@DarwinniwraD
Copy link
Author

yes, try many times, including, reinstall the projects, interchange between yarn and npm, move the config into the package.json file, not working, and the wired thing is, I can find the package under node_modules/ folder

@tommedema
Copy link

Experiencing the same issue. Reinstalled a hundred times to not avail

@alexeyten
Copy link
Contributor

Could you show .git/hooks/pre-commit file?

@tommedema
Copy link

Upgrading to husky@next fixed it for me

@alexeyten
Copy link
Contributor

Can't find Husky message appears in @next (not yet released) version
Looks like you had installed @next and then tried to downgrade to 0.14.3.
I guess, for proper downgrade one have to remove .git/hooks/... files and reinstall husky.

@DarwinniwraD
Copy link
Author

#!/bin/sh
#husky 0.14.3

command_exists () {
  command -v "$1" >/dev/null 2>&1
}

has_hook_script () {
  [ -f package.json ] && cat package.json | grep -q "\"$1\"[[:space:]]*:"
}

cd "."

# Check if precommit script is defined, skip if not
has_hook_script precommit || exit 0

load_nvm () {
  # If nvm is not loaded, load it
  command_exists nvm || {
    export NVM_DIR=/Users/cl/.nvm
    [ -s "$1/nvm.sh" ] && . "$1/nvm.sh"
  }

  # If nvm has been loaded correctly, use project .nvmrc
  command_exists nvm && [ -f .nvmrc ] && nvm use
}

# Add common path where Node can be found
# Brew standard installation path /usr/local/bin
# Node standard installation path /usr/local
export PATH="$PATH:/usr/local/bin:/usr/local"

# nvm path with standard installation
load_nvm /Users/cl/.nvm

# nvm path installed with Brew
load_nvm /usr/local/opt/nvm

# Check that npm exists
command_exists npm || {
  echo >&2 "husky > can't find npm in PATH, skipping precommit script in package.json"
  exit 0
}

# Export Git hook params
export GIT_PARAMS="$*"

# Run npm script
echo "husky > npm run -s precommit (node `node -v`)"
echo

npm run -s precommit || {
  echo
  echo "husky > pre-commit hook failed (add --no-verify to bypass)"
  exit 1
}

@alexeyten this is my pre-commit file

@alexeyten
Copy link
Contributor

What exact error message do you get?

@nathanosdev
Copy link

nathanosdev commented Oct 20, 2018

I was able to fix this issue with:

rm -rf .git/hooks/
npm i -D husky

@emanuel-virca
Copy link

emanuel-virca commented Oct 24, 2018

I got the same error running on windows and it was caused by the fact that the scriptPath contained a white space
Project 2.0/project-app/node_modules/husky/run.js. I do not know much about shell scripts but I have added double quotes wherever the scriptPath was used and it worked.
Bellow is the final pre-commit script.

scriptPath="script path"
hookName=`basename "$0"`
gitParams="$*"

if [ "${HUSKY_DEBUG}" = "true" ]; then
  echo "husky:debug $hookName hook started..."
fi

if [ -f "$scriptPath" ]; then
  node "$scriptPath" $hookName "$gitParams"
else
  echo "Can't find Husky, skipping $hookName hook"
  echo "You can reinstall it using 'npm install husky --save-dev' or delete this hook"
fi

@afuggini
Copy link

rm -rf .git/hooks/ npm i -D husky

This fixed it for me, thanks @AnimaMundi!

@nlindroos
Copy link

In addition to @AnimaMundi's suggestion, I had to remove node_modules.
All steps:

rm -rf .git/hooks/
rm -rf node_modules
yarn

The corrupted hooks were in my case most likely caused by switching from a branch with [email protected] to a branch with [email protected].

@afuggini
Copy link

afuggini commented Oct 30, 2018

I found out that in my case, the problem was that I also had https://www.npmjs.com/package/pre-commit installed, and that overwrote Husky's hooks. I removed this module from packages.json, removed node_modules folder and re-run yarn install. All perfect now.
(Sorry, slightly different issue than this one but thought it might help)

@ianjamieson
Copy link

It recommends to:

npm install husky --save-dev

However, I needed to force the latest version to fix the issue:

npm install husky@latest --save-dev

That is of course if you are ok upgrading to the latest version. This error had occurred after I had just updated a bunch of npm packages, so grabbing the latest was ideal.

@LeeHao12
Copy link

@AnimaMundi work for me , thank you!

@wsrast
Copy link

wsrast commented Mar 12, 2019

Simply uninstalling and reinstalling husky worked for me. My use case is a bit more complex because I'm using a Yarn workspace with husky installed in more than one sub-package, but the uninstall/reinstall cycle in just one of the sub-packages did the trick.

yarn remove husky
yarn add husky --dev

@SandroLuck
Copy link

Was able to solve it by (manually) deleting the node_modules folder and the package-lock.json, then reinstalling. Working on Linux

@reinaldoembarcadores
Copy link

I just deleting the folder .git/hooks/
rm -rf .git/hooks/

working on windows

@canye365
Copy link

@ianjamieson
Thank you very much, he worked for me

@ghost
Copy link

ghost commented Feb 27, 2020

This worked for me. In package.json

"husky": {
"hooks": {
"pre-commit": "npm test",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},

@remjx
Copy link

remjx commented Mar 21, 2020

My server was still running which was locking node_modules. Once I shut down the server it installed successfully.

@danielrussellLA
Copy link

danielrussellLA commented Aug 12, 2020

look for your .git folder - it is likely in the root of your project - then remove the pre-commit hook file with rm .git/hooks/pre-commit.

if you cat .git/hooks/pre-commit and you've used husky before - the pre-commit file will likely look like auto-generated bash code. feel free to delete it

@stoicskyline
Copy link

stoicskyline commented Aug 17, 2020

For Yarn:
Running yarn add husky may not display an error, for example when your Git is out of date, which will silently skip installing Husky's Git hooks. So best to use npm first to setup the Git hooks:

rm -rf .git/hooks
rm -rf node_modules/

brew install git
npm i -D husky
ls .git/hooks

rm -rf node_modules
yarn install

@eyedean
Copy link

eyedean commented Sep 15, 2020

I believe it is because husky attaches its hooks from .huskyrc and only at the time of installation. So, if you try to first install husky (via npm i or npm i -D husky) and then create .huskyrc file, it fails!

You must reinstall husky, after .huskyrc is created (and possibly every time it is modified). That's why rm -rf .git/hooks && npm i -D husky is the solution. :)

PS. If you don't want to delete all the hooks, you can simply rm -f .git/hooks/pre-commit for pre-commit hook.

PPS. I am back to here after four years (in 2024)! Here is what I needed to do:

npm un husky && rm -f .git/hooks/pre-commit && npm i -D husky && cat .git/hooks/pre-commit

@OnkelTem
Copy link

2021, March. A receipt from 2018 has helped me.
#333 (comment)
Thanks.

niklaas added a commit to niklaas/ng-table-cu that referenced this issue Apr 18, 2021
b/c husky-run is missing and suggested at [1].

[1]: typicode/husky#333 (comment)
@fashen007
Copy link

cd .git && rm -rf hooks
npm i -D [email protected]
npm i -D husky

work for me~

@Adarsh-thedev
Copy link

If you are using WSL with windows and not able to solve this issue with any of the above methods, try to commit all your changes with the ubuntu terminal (WSL).
-> If it freezes during the process, simply terminate the process and run git stash
then try to commit your changes and it should work.

@juliusaboagye
Copy link

Shortening the file path in windows saved me. Thanks

@Waseem1223
Copy link

I was able to fix this issue with:

rm -rf .git/hooks/
npm i -D husky

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