Mon Jan 29 2024 2 Min read

How to Install Commitlint & Husky [2024]


thumbnail

Commitlint is a linter tool that checks commit messages against a set of rules, helping to maintain consistency and clarity in a project’s versioning and changelog generation. In simpler terms, it will display errors if your message doesn’t follow the proper rules of conventional commit messages.

Commitlint is triggered by Husky, a tool that manages Git hooks and runs the validation script automatically before committing. Husky recently had a major update (v9) that deprecated certain commands, therefore this blog post serves as a guideline to install Commitlint according to the new procedure required.

How to Install Commitlint & Husky

Commitlint

The installation of Commitlint stayed the same.

First, we install the Commitlint CLI and the Conventional Commits configuration as development dependencies:

npm install @commitlint/cli @commitlint/config-conventional --save-dev

Then, we need to extend the Commitlint conventional configuration:

echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

Husky

Now, the new configuration of Husky doesn’t let you use the npx husky install command. If you try to use that with a version that is 9.0.0 or above, you will get the following error:

husky_install_error

There are two ways to address this problem:

  • You can change Husky’s version in the package.json file. If you modify the version number from 9.x.x to 8.x.x and then run npm install, the deprecated commands will work properly.
  • The other—and recommended—method is to adapt the new commands so that later you won’t have any problems when updating the dependency.

To install Husky from the version 9.0.0 and above, you’ll have to run the following command:

npx husky init

After installing the dependency, you will have to create a commit-msg file in the .husky folder to add commit message linting to commit-msg hook.

It can be done in a single line of code:

echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg

Now, at this stage, you will still bump into a problem. Husky has generated a file for you that—if you’ve followed the same path that I have—will also produce some errors when trying to commit a change:

husky_no_test.svg

You can find a file with the name pre-commit in the .husky folder. If you simply remove that (rm pre-commit), or change its content to a command with an existing script, the problem will go away and you will be free to go using Commitlint.

commitlint_in_use.svg

You can also check the Husky official documentation for more information on how to use this new version.

We don’t rise to the level of our expectations/hopes, we fall to the level of our training. – Archilochus
Sample picture caption

Sample paragraph title

for i in range 10

The success of any open source project relies on the collaboration and participation of individuals from all over the world. In order to foster a healthy and productive open source community, it's crucial to prioritize empathy and kindness towards one another.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto distinctio culpa ipsum consectetur, est adipisci voluptatibus sint odit quos totam laborum ad, enim nihil. Dolores consequatur aspernatur enim ratione. Possimus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius quis rem, saepe magni quia officiis voluptatum dolorem debitis corrupti optio ex sint ipsum consectetur adipisci nam, quibusdam architecto iste doloribus? Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni quam vitae excepturi vel neque illo deserunt pariatur odit exercitationem eveniet esse sit, quia voluptatibus inventore nihil nemo! Modi, corporis dolore? Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur temporibus adipisci minima! Optio earum iusto deserunt, harum commodi pariatur eligendi repellendus libero, quas, beatae facere minus? Quidem deleniti autem amet!

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto distinctio culpa ipsum consectetur, est adipisci voluptatibus sint odit quos totam laborum ad, enim nihil. Dolores consequatur aspernatur enim ratione. Possimus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius quis rem, saepe magni quia officiis voluptatum dolorem debitis corrupti optio ex sint ipsum consectetur adipisci nam, quibusdam architecto iste doloribus? Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni quam vitae excepturi vel neque illo deserunt pariatur odit exercitationem eveniet esse sit, quia voluptatibus inventore nihil nemo! Modi, corporis dolore? Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur temporibus adipisci minima! Optio earum iusto deserunt, harum commodi pariatur eligendi repellendus libero, quas, beatae facere minus? Quidem deleniti autem amet!

Concluding Remarks

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto distinctio culpa ipsum consectetur, est adipisci voluptatibus sint odit quos totam laborum ad, enim nihil. Dolores consequatur aspernatur enim ratione. Possimus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius quis rem, saepe magni quia officiis voluptatum dolorem debitis corrupti optio ex sint ipsum consectetur adipisci nam, quibusdam architecto iste doloribus? Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni quam vitae excepturi vel neque illo deserunt pariatur odit exercitationem eveniet esse sit, quia voluptatibus inventore nihil nemo! Modi, corporis dolore? Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur temporibus adipisci minima! Optio earum iusto deserunt, harum commodi pariatur eligendi repellendus libero, quas, beatae facere minus? Quidem deleniti autem amet!