'How to fix error 'not found husky-run' when committing new code?

When committing on a project that uses Husky, I get an error that says not found husky-run

I checked the package.json and it has husky as a dependency, and I can see the pre-commit hook configuration for Husky in the package.json. So I don't know what to do to fix this. Additionally, other members on my team can commit and husky works for them.

I also tried rm -rf node_modules && npm install and then committing again, but still, I get the same error.

Anyone else have ideas on how to fix this?



Solution 1:[1]

To fix this there are two methods, depending on which version of Husky you are already on.

If you're using Husky v4 or lower, do the following:

rm -rf .git/hooks
npm install

For Husky v7 or greater, do the following:

# For NPM
 npm install husky@7 --save-dev \
      && npx husky-init \
      && npm exec -- github:typicode/husky-4-to-7 --remove-v4-config

# For Yarn
 yarn add husky@7 --dev \
  && npx husky-init \
  && npm exec -- github:typicode/husky-4-to-7 --remove-v4-config
# or
 yarn add husky@7 --dev \
  && yarn dlx husky-init --yarn2 \
  && npm exec -- github:typicode/husky-4-to-7 --remove-v4-config

At this point you should be able to commit and have your hooks working again.

If anything goes wrong, please read the documentation for migration from 4 to 7.

Solution 2:[2]

To fix this in husky version 6 run:

yarn husky install

Solution 3:[3]

Do not delete .get/hooks hooks won't work. According migrating manual from 4 to 6 version :

For npm usage execute

 npm install husky@6 --save-dev \
      && npx husky-init \
      && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config

For yarn usage:

 yarn add husky@6 --dev \
  && npx husky-init \
  && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config

and

yarn add husky@6 --dev \
  && yarn dlx husky-init --yarn2 \
  && npm exec -- github:typicode/husky-4-to-6 --remove-v4-config

If any errors while the process you can simply revert changes by executing:

rm -rf .husky && git config --unset core.hooksPath

Explanation what's going on:

husky init sets up Git hooks and updates your package.json scripts (you may want to commit your changes to package.json before running husky init).

husky-4-to-6 creates hooks based on your husky v4 config. If --remove-v4-config is passed, previous config will be deleted (recommended).

Solution 4:[4]

I simply had to add a prepare script to my package.json:

"scripts": {
  ...
  "prepare": "husky install",
  ...
}

Then run yarn install or npm install and husky will be initialized. This will make sure people who check out your repo will also be able to run husky.

Solution 5:[5]

just a "yarn install" solved this for me

Solution 6:[6]

just this 'yarn add husky@6 --dev' inside your terminal

Solution 7:[7]

This worked for me:

add a file ~/.huskyrc if you don't have one already

  • touch ~/.huskyrc
  • open ~/.huskyrc
  • paste the following:
# ~/.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"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PURGEN
Solution 2 Zeeshan Ali
Solution 3 marc_s
Solution 4
Solution 5 Ravi Kumar
Solution 6 Owamamwen
Solution 7 Ayman Bakri