husky icon indicating copy to clipboard operation
husky copied to clipboard

Why do we need to create the `"prepare": "husky install"` script for `package.json`?

Open CodingInvoker opened this issue 5 years ago • 21 comments

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 image

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?

CodingInvoker avatar Jul 30 '21 03:07 CodingInvoker

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.

Ryuno-Ki avatar Jul 30 '21 18:07 Ryuno-Ki

This is always the dilemma of

  1. Everything just works without the required knowledge
  2. 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

yordis avatar Jul 31 '21 20:07 yordis

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.

ElijahLynn avatar Aug 02 '21 22:08 ElijahLynn

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.

image

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:

  1. there's no need to have an extra step of npm set-script prepare "husky install" for everyone to set up husky.
  2. the package.json can get rid of "prepare": "husky install".
  3. 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.sh is 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!

CodingInvoker avatar Aug 19 '21 12:08 CodingInvoker

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.

stale[bot] avatar Oct 19 '21 01:10 stale[bot]

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.

image

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:

  1. there's no need to have an extra step of npm set-script prepare "husky install" for everyone to set up husky.
  2. the package.json can get rid of "prepare": "husky install".
  3. 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.sh is 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"

Dathin avatar Oct 20 '21 23:10 Dathin

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.

mmounts avatar Nov 01 '21 18:11 mmounts

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.

stale[bot] avatar Dec 31 '21 19:12 stale[bot]

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

image

Sandeep-Malhotra avatar May 25 '23 00:05 Sandeep-Malhotra

See https://typicode.github.io/husky/guide.html#disable-husky-in-ci-docker-prod

typicode avatar May 25 '23 12:05 typicode

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?

GermanJablo avatar Jul 04 '23 04:07 GermanJablo

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.

ryansand20 avatar Oct 17 '23 21:10 ryansand20

+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?

benallfree avatar Oct 31 '23 05:10 benallfree

I think it's related to this https://typicode.github.io/husky/guide.html#custom-directory Next version will be more relaxed about this.

typicode avatar Oct 31 '23 21:10 typicode

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.

jasonkarns avatar Dec 09 '24 17:12 jasonkarns

git config core.hooksPath .husky is really needed as husky command sets core.hooksPath to .husky/_

sKopheK avatar Feb 20 '25 16:02 sKopheK

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)

jasonkarns avatar Feb 22 '25 13:02 jasonkarns

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 husky from the prepare script
  • Create a new setup script that runs husky
  • 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?

PaulRBerg avatar May 23 '25 13:05 PaulRBerg

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?

Image

josefanostylus avatar Jun 01 '25 19:06 josefanostylus

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

Ryuno-Ki avatar Jun 02 '25 06:06 Ryuno-Ki