👈 Back

Make Husky Pre-Commit Hooks Opt-In

Thu Dec 01 2022

Making your pre-commit hooks opt-in is usually a good idea. Most JavaScript/TypeScript/Node.js projects seem to have standardised on Husky as the means of achieving this.

Luckily, making Husky opt-in is really simple. When you set up Husky, you likely added the following script to your package.json:

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

prepare is a special script in Node projects that will auto-run whenever you run npm install locally (without any arguments). Because of this, it is likely everyone who uses the project will have Husky auto-installed.

Let’s rename this script to something else so it doesn’t auto-run, I usually like install-hooks.

"scripts": {
  "install-hooks": "husky install",
  ...
}

And that’s it. Seriously. Husky does nothing if it isn’t installed locally.

Now your team have individual control over whether they want to opt-in to the Husky hooks or not.

The only other thing you’re likely to want to do is update your README.md to inform others of how to set up the hooks if they do want to use them…