Why do we need to create the `"prepare": "husky install"` script for `package.json`?
Context This is acutally not a problem while using husky, but rather than a question about npm documentation: https://www.npmjs.com/package/husky
In the documentation, it says that we need to run scripts as below

Is there reason that we want to preserve the script "prepare": "husky install" in package.json? I mean it seems like this is only a one time setup scirpt, any benefit of keeping it in package.json?
If we don't need to preserve it in package.json's script field, should it be more straightforward to run npx husky install rather than add a "prepare": "husky install" script firstly and run npm run prepare?
In my experience, it's a one-off script. It's easier to remind contributors to run this script once to set up their system accordingly.
This is always the dilemma of
- Everything just works without the required knowledge
- I need to learn how things work
If people forget to run the script by, you know, RTFM™️, then what? There is a reason why RTFM exists honestly
I just encountered this (as a devops engineer, not a node engineer) and it seems that the prepare section is implicitly invoked by npm/yarn install so therefore it appears that the intent is that when one runs yarn install it will run husky install and I don't need to update my CI scripts etc.
Currently(husky v7.0.1), what husky install will do is to add some files as the screenshot below. I was under the impression that these files should also be uploaded to your project repo so that everyone else working on the same project will just have husky working out of box. The thing is gitignore file .husky/_/.gitignore is making our git ignore the generated .husky/_/husky.sh script file.
With that script file being ignored, I suppose we do need the "prepare": "husky install" to exist in the package.json, so that everytime when someone pull the code, he must run npm install firstly, and this will trigger the npm Life Cycle Scripts(in our case: "prepare": "husky install") adding the ignored .husky/_/husky.sh script file locally.

If we can change the behavior of husky install to not generate that ignore file, so that the .husky/_/husky.sh can be uploaded to each project/repo, then I suppose we can have the following benefits:
- there's no need to have an extra step of
npm set-script prepare "husky install"for everyone to set up husky. - the package.json can get rid of
"prepare": "husky install". - everyone else working on the same project still have things to work out of box after pulling the code from remote, because that
.husky/_/husky.shis a tracked file.
But yeah I guess an obvious drawback is that we are adding more stuff to our project(the script file), which might be the original reason of ignoring the script file so that the "prepare": "husky install" is needed.
At the end of the day it is only a matter of preference, but as long as we understand what is going on under the hood, shouldn't make too much differences.
Thank everyone for joining this discussion!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Currently(husky v7.0.1), what
husky installwill do is to add some files as the screenshot below. I was under the impression that these files should also be uploaded to your project repo so that everyone else working on the same project will just have husky working out of box. The thing is gitignore file.husky/_/.gitignoreis making our git ignore the generated.husky/_/husky.shscript file.With that script file being ignored, I suppose we do need the
"prepare": "husky install"to exist in thepackage.json, so that everytime when someone pull the code, he must runnpm installfirstly, and this will trigger the npm Life Cycle Scripts(in our case:"prepare": "husky install") adding the ignored.husky/_/husky.shscript file locally.
If we can change the behavior of
husky installto not generate that ignore file, so that the.husky/_/husky.shcan be uploaded to each project/repo, then I suppose we can have the following benefits:
- there's no need to have an extra step of
npm set-script prepare "husky install"for everyone to set up husky.- the package.json can get rid of
"prepare": "husky install".- everyone else working on the same project still have things to work out of box after pulling the code from remote, because that
.husky/_/husky.shis a tracked file.But yeah I guess an obvious drawback is that we are adding more stuff to our project(the script file), which might be the original reason of ignoring the script file so that the
"prepare": "husky install"is needed.At the end of the day it is only a matter of preference, but as long as we understand what is going on under the hood, shouldn't make too much differences.
Thank everyone for joining this discussion!
Even doing this there were still being one pending step missing. Which is run "git config core.hooksPath .husky"
Just ran into an issue related to this and would love to know the reasoning as well.
For context my team just started using the latest version of Husky (v7) and immediately ran into an issue using npm ci --production for our builds.
Now I understand there have been workarounds published: https://github.com/typicode/husky/issues/914, https://typicode.github.io/husky/#/?id=with-npm
But is there an actual reason that these workarounds are being considered the default, correct behavior? Husky is by design a local-dev-only tool, which is why it's installed in devDependencies here. So why are users then instructed to save the install script as an NPM lifecycle script (prepare) that is run by default by commands that are intended for CI/CD pipelines (like npm ci) and then given workarounds to make those pipelines continue functioning? This seems like it's directly against what the lifecycle method was designed for and should not be encouraged, in case other packages do the same.
It seems pretty backwards compared to either: a) instructing users to set up husky install as part of a non-lifecycle NPM script, like setup, and instruct new developers to run it at least once, or b) making the husky install obsolete.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I am facing the same issue. I am trying the SAP MTA tool. Deploying the code on Cloud Foundry.
Have tried all the options mentioned in the documentation but no success
See https://typicode.github.io/husky/guide.html#disable-husky-in-ci-docker-prod
I also found the "prepare" script unnecessary. But seeing that husky is a standard with more than 7m weekly downloads, I thought there was a good reason that I didn't know about.
Now reading this thread I'm starting to suspect that maybe I wasn't wrong after all.
I use Husky because I trusted its claim that it adds syntactic sugar, improves DX, and helps avoid common pitfalls. However, following that reasoning, I now wonder if it actually offers any advantage over using Git hooks directly.
Again, husky is so popular and ubiquitous that it makes me think I'm missing something. Could someone explain to me?
We had a problem where "npm publish" was running the "prepare" step from the published dist folder. Obviously the .git folder doesn't exist there, so we see this error: ".git folder not found"
We fixed the ability to publish by changing our prepare step to the following: "prepare": "[ -d '.git' ] && husky install || echo 'skipping husky'"
This checks for the .git folder before trying to setup husky.
+1 for @ryansand20's solution. I was hitting the same problem, except in dependencies. I'm not sure how anyone is publishing a package with "prepare": "husky install" when husky is a devDependency. Is there a better npm hook?
I think it's related to this https://typicode.github.io/husky/guide.html#custom-directory Next version will be more relaxed about this.
Same concerns as @benallfree. I'm thinking this might be a bug? Husky shouldn't be trying to install during prepare because packages that use husky can then not be installed as git-deps in other projects.
git config core.hooksPath .husky is really needed as husky command sets core.hooksPath to .husky/_
This issue is closed and clearly being ignored. We need to open a new issue to get this fixed apparently (and actively fight the stale bot)
The real issue here is that npm doesn't provide a lifecycle script that is run upon running npm install locally (as a developer/contributor), but NOT when external users install the package.
The only solution for now seems to be to:
- Remove
huskyfrom thepreparescript - Create a new
setupscript that runshusky - Inform your contributors about the necessity of running
setup
In the meantime, can one of the Husky contributors confirm that by having husky in the prepare script, we override our users' git hooks when our package is installed through git?
See https://typicode.github.io/husky/guide.html#disable-husky-in-ci-docker-prod
This link is not working. Could you please provide updated link?
I found it in Wayback Machine: https://web.archive.org/web/20231207190412/https://typicode.github.io/husky/guide.html#disable-husky-in-ci-docker-prod