Integrate Commitizen with your node.js project
Tags: node, commitizen, git, convention
Published: 2021-03-30 19:53:27

When are working on several projects a common problem could be the different conventions or standardization with the commits, to that kind of problem can be used Commitizen which will create an easy and good integration with your projects. 

A benefit of the standardization is related to generate an automatic changelog or release notes for the project.

When you commit with Commitizen, you'll be prompted to fill out any required commit fields at commit time.

## Setup Commitizen

First of all, we need to install dependencies in the project.

```shell
npx commitizen init cz-conventional-changelog --save-dev --save-exact
```

The above command does three things for you:
1. Installs the cz-conventional-changelog adapter npm module
2. Saves it to package.json's dependencies or devDependenciesAdds
3. The config.commitizen key to the root of your package.json as shown here

```json
{
  ...
  "config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
  }
}
```

Alternatively, commitizen configs may be added to a .czrc file:
```json
{
  "path": "cz-conventional-changelog"
}
```

After that, you can add some new npm run scripts in your package.json

```json
{
  ...
  "scripts": {
    "commit": "cz"
  }
}
```
This will be more convenient for your users because then if they want to do a commit, all they need to do is run `npm run commit` and they will get the prompts needed to start a commit!
## Commitizen Questions
1. Select the type of change that you're committing: feat:     A new feature? (For more information see the Commitizen type changes section)
2. What is the scope of this change (e.g. component or filename): (press enter to skip)
3. Write a short, imperative tense description of the change (max 82 chars):
4. Provide a longer description of the change: (press enter to skip):
5. Are there any breaking changes? **yes/no**
6. Does this change affect any open issues? **yes/no**

### Commitizen type changes

| Type | Description |
|----------|-------------|
| feat |  A new feature |
| fix |    A bug fix   |
| docs | Documentation only changes |
| style | Changes that do not affect the meaning of the code |
| refactor | A code change that neither fixes a bug nor adds a feature |
| perf | A code change that improves performance |
| test | Adding missing tests or correcting existing tests |
| build | Changes that affect the build system or external dependencies |
| ci | Changes to our CI configuration files and scripts |
| chore | Other changes that don't modify src or test files |
| revert | Reverts a previous commit |

## Running Commitizen on git commit (Optional)

### Traditional git hooks
Update .git/hooks/prepare-commit-msg with the following code:

```shell
exec < /dev/tty && node_modules/.bin/cz --hook || true
```

### Husky
For husky users, add the following configuration to the project's package.json:

```shell
{
  ...
  "husky": {
    "hooks": {
      "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true",
    }
  }
}
```

Was created a new post for the automatic changelogs or release notes process for your project. [How to Automatically Generate Changelog for your node.js projects](https://dev.to/brayanarrieta/how-to-automatically-generate-changelog-for-your-node-js-projects-43jk)

## References
- [How to Automatically Generate Changelog for your node.js projects](https://dev.to/brayanarrieta/how-to-automatically-generate-changelog-for-your-node-js-projects-43jk)
- [Commitizen](https://www.npmjs.com/package/commitizen)