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

12

67

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?

Pyramid answered 17/2, 2021 at 17:0 Comment(1)
I got this after running yarn install inside a unix-based Docker container and then committing on Windows. Windows was unable to run the file, even thought it existed.Shit
P
116

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.

Pyramid answered 17/2, 2021 at 17:0 Comment(7)
Thanks, it worked for me, npm install step wasn't required. Using yarn hereNegate
@dspacejs actually this solution worked for yarn too. Try to first delete your node_modules manually then reinstall.Luger
I'm always wary of solution with command line starting by rm -rf. Nuking a whole directory sound a bit much, especially when there is a migration script.Manor
this didn't work for me, git hooks directory was not recreatedEnhance
OK so this might have worked just fine for husky version 4 or older, but if you've upgraded to husky 6 then you need to follow the answer from Uladz Kha below.Enhance
this did it for mePelson
Thankyou - worked a treat - FYI use rm -Recurse -Force .git/hooks for PowershellDecide
R
18

To fix this in husky version 6 run:

yarn husky install
Rh answered 30/4, 2021 at 12:12 Comment(0)
A
10

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).

Antonantone answered 21/4, 2021 at 8:27 Comment(5)
FYI People, you need npm version 7 to run the last script in this thing.Farlee
I concur, needed npm version 7 to get this to work. After I ran the scripts, I reverted to npm 6 and nodejs 12 and things are still functioning correctly for me. Also this section of their help docs can help you update your scripts to work with husky@6: typicode.github.io/husky/#/?id=migrate-from-v4-to-v6Enhance
i have npm 6 and node 10 beause of project limitations. probably thats why it broke for me...Pelson
please try to yarn husky install for npm 6Antonantone
ALSO make sure you are on git version 2.9 or above ✅ (I was not and Husky 6 was not working after going through the migration cli steps). Found this little detail tucked in the fine print of the Husky migration docs and this resolved it.Mussorgsky
F
8

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.

Frady answered 11/12, 2021 at 0:0 Comment(0)
M
6

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"
Matazzoni answered 8/5, 2022 at 21:24 Comment(1)
This one worked for me with husky@4 TyHedden
I
5

just a "yarn install" solved this for me

Irrespective answered 19/5, 2021 at 11:56 Comment(0)
S
3

I occur this problem because the .git folder is at a different level from the package.json file, I resolved it by referencing this document:

https://typicode.github.io/husky/#/?id=custom-directory

Just like this:

// package.json
{
  "scripts": {
    "prepare": "cd .. && husky install front/.husky"
  }
}
Scurry answered 3/11, 2022 at 3:44 Comment(0)
S
0

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

Sendal answered 23/11, 2021 at 22:49 Comment(0)
P
0

If it may help anyone experiencing this issue and using yarn, uninstalling and reinstalling the husky package has fixed it in my case.

(Please check if you would need -W flag else remove it from the commands)

uninstall:

 yarn remove husky -W

install:

 yarn add husky@^4.2.5 -W //to install a specific version
 
 (or)
 
 yarn add husky -W //to install the latest version
Presentationism answered 15/6, 2022 at 15:55 Comment(0)
Q
0

Adding to Ayman Bakri's answer, and based on this gist, if you are using a git frontend like Fork together with asdf and you are getting errors like npx: command not found, then you have to source asdf.sh in your ~/.huskyrc as follows:

source /usr/local/opt/asdf/libexec/asdf.sh

if you installed asdf via a package manager like homebrew, or:

source ~/.asdf/asdf.sh

if you installed asdf using the instructions. You must source the bash compatible script asdf.sh even if your default shell is Fish.

Quinta answered 12/4, 2023 at 0:14 Comment(0)
S
0

My NODE_ENV was set to production. Unsetting it, then rerunning npm i fixed it for me.

Somnus answered 28/11, 2023 at 15:41 Comment(0)
A
0

If you think you have all well configured in your project and still having this issue, ensure you also install yarn globally:

npm install --global yarn

Some terminals, consoles or other external tools like GitHub desktop are no using the local yarn, but the global.

Atelectasis answered 7/3, 2024 at 1:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.