paint-brush
Setting Up Husky 8 With Lernaby@suryarajendhran
1,162 reads
1,162 reads

Setting Up Husky 8 With Lerna

by Surya RajendhranJune 15th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Learn everything you need to know about setting up Husky 8 with Lerna in this useful step-by-step guide.

People Mentioned

Mention Thumbnail
featured image - Setting Up Husky 8 With Lerna
Surya Rajendhran HackerNoon profile picture

So I went into a rabbit hole of setting up Husky’s git hooks with Lerna and realized that there wasn’t a straightforward guide to setting up the latest version of Husky with Lerna. So here goes.


The following setup steps are yarn specific. You can replace yarn with npm for step 1 and replace yarn with npx for all the other steps.

1. Install Husky

yarn add --dev -W husky


Install Husky as a dev dependency on your project root

2. Install Husky’s git hooks

yarn husky install


Git hooks are actually a Git feature and not a Husky feature (something I did not know). So when you run the above command, Husky will change the git hooks directory from .git/hooks in your project root (where you will find samples from git about what you can do with hooks) to .husky/. You can read more about what git hooks are available here.


You can verify that this install was successful by checking if the hook’s path was successfully changed

git config core.hooksPath

Should return .husky

3. Test if hooks are being called

You can customize a git hook in Husky using the following command:

yarn husky add .husky/pre-commit "yarn test"

You can now see that a new file has been created .husky/pre-commit. To quickly verify that this is indeed working, you can edit the file to print something and exit.

echo "Hello World"
exit 1

Now add and commit the files that were created in your .husky directory to test the pre-commit hooks.

git add .husky/pre-commit
git commit -m "Test commit"

This should print “Hello World” and then not commit the files (because we returned exit code 1 from the pre-commit hook).

4. Supporting git hooks for multiple packages

You can set up the root pre-commit and pre-push hooks in such a way that it works well with multiple packages.


From the root of the project, add pre-commit and pre-push hooks:

yarn husky add .husky/pre-commit "yarn lerna run pre-commit"
yarn husky add .husky/pre-push "yarn lerna run pre-push"

This allows you to setup package specific pre-commit and pre-push steps by adding to the packages scripts in the package.json like this:

// packages/package-1/package.json
{
  ...
  "scripts": {
    ...
    "pre-push": "yarn lint-changed; yarn test-changed",
    ...
  }
  ...
}


5. Setting up commitlint to lint your commit messages

Install commitlint along with lerna specific peer dependencies

yarn add --dev -W @commitlint/cli @commitlint/config-conventional \\
@commitlint/config-lerna-scopes

Add the commit-message hook to husky

yarn husky add .husky/commit-message "yarn commitlint --edit $1"