How I setup ESLint, Prettier and Commit Linting
As a software developer, maintaining the consistency and quality of my code is something I always keep on top of. One way I ensure this is through the use of linting. Today, I’d like to walk you through my process of setting up code linting, with a focus on commit linting. We will do this using ESLint, Prettier, Husky, and lint-staged.
Code Linting
Section titled Code LintingLinting is a method to automatically check your code for potential errors. It allows you to follow coding standards and avoid common mistakes.
Steps
Section titled Steps1. Installing ESLint and Prettier
Section titled 1. Installing ESLint and PrettierTo begin with, we need to install ESLint, Prettier, and eslint-config-prettier. ESLint helps in identifying and reporting on patterns found in ECMAScript/JavaScript code. Prettier is an opinionated code formatter, and eslint-config-prettier is a config that disables ESLint rules that conflict with Prettier.
In your project directory, use the following npm command to install these as dev dependencies:
2. Initialise ESLint
Section titled 2. Initialise ESLintNext, let’s initialise ESLint with this command:
You’ll be asked a few questions. When asked if you want to install ‘eslint-config’, answer ‘Yes’. For the primary purpose of your configuration, choose ‘To check syntax and find problems’. Answer the following questions based on your project.
3. Configure ESLint to Use Prettier
Section titled 3. Configure ESLint to Use PrettierIn your ESLint configuration file, add “prettier” to the extends array:
4. Configure Prettier
Section titled 4. Configure PrettierTo prevent certain files from being formatted by Prettier, add a .prettierignore file in your root directory:
Then, add a .prettierrc file to configure Prettier. Here’s an example:
5. Add Prettier and ESLint to Your Scripts
Section titled 5. Add Prettier and ESLint to Your ScriptsAdd the following lines to the “scripts” section of your package.json file:
Commit Linting
Section titled Commit LintingCommit linting helps to enforce a consistent commit style, which is particularly useful when you want to automate tasks like releasing and generating changelogs.
6. Install and Initialise Husky
Section titled 6. Install and Initialise HuskyHusky is a tool that lets you run scripts when specific Git events happen (like pre-commit, post-commit, etc.). Install it and initialise it using:
7. Install lint-staged
Section titled 7. Install lint-stagedlint-staged runs linters on staged files. Install it with:
Then, add a lint-staged configuration to your package.json file:
8. Set Up Husky with lint-staged
Section titled 8. Set Up Husky with lint-stagedNext, you need to set up Husky to use lint-staged whenever a commit is made. This is accomplished by adding a pre-commit hook. Use this command:
After running the above command, a pre-commit file will be generated in the .husky directory. Open this file and ensure it contains the command “npx lint-staged”. If you see a “test” command, you can remove it.
9. Install Commitizen
Section titled 9. Install CommitizenCommitizen is a tool that makes creating commits following a standard format easier. To install Commitizen and initialize it with the cz-conventional-changelog adapter, use the following command:
The cz-conventional-changelog is an adapter that follows the conventional changelog specification. However, you can replace it with an adapter of your choosing.
10. Add Prepare Commit Hook
Section titled 10. Add Prepare Commit HookFinally, add a hook for preparing commit messages with Commitizen. Here’s the command:
This command will create a prepare-commit-msg file in the .husky directory. This ensures that every time you commit, you are prompted to follow the commit conventions.
And that’s it! You now have ESLint for checking your code, Prettier for formatting it, and lint-staged with Husky for managing your commits, all set up and ready to go. Happy coding!