typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
32.34k stars 1.02k forks source link

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

Closed DarwinniwraD closed 5 years ago

DarwinniwraD commented 6 years ago

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 commented 6 years ago

Have you tried to do what error message suggests?

DarwinniwraD commented 6 years ago

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 commented 6 years ago

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

alexeyten commented 6 years ago

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

tommedema commented 6 years ago

Upgrading to husky@next fixed it for me

alexeyten commented 6 years ago

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 commented 6 years ago
#!/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 commented 6 years ago

What exact error message do you get?

nathanosdev commented 5 years ago

I was able to fix this issue with:

rm -rf .git/hooks/
npm i -D husky
emanuel-virca commented 5 years ago

I got the same error running on windows and it was caused by the fact that the scriptPathcontained 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 commented 5 years ago

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

This fixed it for me, thanks @AnimaMundi!

nlindroos commented 5 years ago

In addition to @AnimaMundi's suggestion, I had to remove _nodemodules. 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 husky@1.1.2 to a branch with husky@0.14.3.

afuggini commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@AnimaMundi work for me , thank you!

wsrast commented 5 years ago

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 commented 5 years ago

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

reinaldoembarcadores commented 5 years ago

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

working on windows

canye365 commented 4 years ago

@ianjamieson Thank you very much, he worked for me

ghost commented 4 years ago

This worked for me. In package.json

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

remjx commented 4 years ago

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

danielrussellLA commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

2021, March. A receipt from 2018 has helped me. https://github.com/typicode/husky/issues/333#issuecomment-431591451 Thanks.

fashen007 commented 3 years ago
cd .git && rm -rf hooks
npm i -D husky@4.3.8
npm i -D husky

work for me~

Adarsh-thedev commented 3 years ago

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 commented 2 years ago

Shortening the file path in windows saved me. Thanks

Waseem1223 commented 2 years ago

I was able to fix this issue with:

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