![]()
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:
There are two ways to address this problem:
- You can change Husky’s version in the
package.jsonfile. If you modify the version number from9.x.xto8.x.xand then runnpm 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:
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.
The best code is the code you can still understand when the deadline gets loud.
