Commit-lint for your node project

Kavindu Vindika
3 min readOct 28, 2019

--

If you’re working with a team for your project, it’s most probable that you’re using git and git-hub for version control. Since each one in your team could have a different perspective about the project. Some are careful with their commit messages and PR template. But some could have ignored it mistakenly. Therefore it’s better to have a particular strategy for commit messages in your project to make it more clearer and understandable for anyone who could have exposed to your code base. And this becomes so much important when it comes to open source projects. Developers all around the world could’ve contributed in your project and it could not be guaranteed that everyone will adhere to a certain way of committing their messages. Therefore you can restrict to do their commit messages in a proper manner.

In order to do that, install the following.

npm install -S -D @commitlint/cli @commitlint/config-conventional husky

Since commit-lint is only relevant for development, be careful to add it as a dev dependency as above.

Then create commitlint.config.js file as follows.

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

Then add the following tag in your package.json file to enable commit-lint.

"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}

Now you’ve successfully enabled commit-lint in your node project.

Now following commit convention should be used for the commit messages.
e.g. : type : subject
*type should be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]
*subject must not be sentence-case, start-case, pascal-case, upper-case.

Let’s try to commit and see the output.

Here you can see without subject or type, your commit won’t be valid and accepted.

Type of the commit explanation…

test: Adding non-written tests

feat: Introduce a new feature

fix: A bug fix

chore: Build process or auxiliary tool changes

docs: Documentation changes only

refactor: A code change that neither fixes a bug or add a feature

style: Markup, white-space, formatting or UI development

Here you can see without a valid type given, you can’t commit.

Above errors will occur, if you avoid commit-lint rules.

Now let’s try to commit properly and see the output.

Now onward commit-lint will save you from unnecessary commit messages…!

--

--

Kavindu Vindika

AWS Certified Solution Architect - Associate | Associate Technical Lead @SyscoLABS | AWS Community Builder