NodeJS – Auto Lint & Format on Git Commit with Airbnb Style Guide

When you’re working in a team, each developer will have their own style. It’s very important to have a consistent style across all the files.

Looking at a piece of code, you shouldn’t be able to tell who wrote it ?.

With this guide, you’ll be able to set up auto linting and formatting on Git commit.

If you’re a ReactJS developer, read this – ReactJS – Auto Lint & Format on Git Commit with Airbnb Style Guide

It’s divided into 5 parts

You’ll learn

  1. Setup Eslint with Airbnb Style Guide
  2. Setup Formatting with Prettier
  3. Auto Lint & Format on Git Commit
  4. Configure VS Code for Eslint and Prettier
  5. Additional Eslint rules for NodeJS

Why you’ll need Linting and Formatting?

  • Clean code
  • Easily find errors, typos, syntax errors
  • Follow the best practices
  • Warning on using deprecated/harmful methods
  • Have a consistent style in code across the team
  • Avoid committing bad code like console.log
  • Make PR awesome!

Setup Eslint with Airbnb Style Guide

Eslint is a linting utility for JavaScript and JSX, with some nice rules and plugins. Anyone can write rules for Eslint. An example rule could be “avoid using console.log()“

Luckily Airbnb has written a Style Guide for JavaScript which covers most of the best practices they use. It’s basically a collection of different rules. You can read it here – Airbnb JavaScript Style Guide

Step 1  – Install necessary packages by npm i -D eslint eslint-config-airbnb-base eslint-plugin-import eslint-plugin-promise

Step 2 – Create a new file .eslintrc at the root directory of your project and paste the following

Step 3 – Add a new command to lint in package.json"lint": "eslint 'src/**/*.js' --fix"

Now you should be able to able lint your code by running npm run lint. It will try to fix errors that are fixable. Otherwise will throw errors/warnings

Setup Formatting with Prettier

While Eslint is for Linting and finding errors in the code, Prettier is purely for formatting. Besides JavaScript Prettier also supports formatting json, HTML, CSS, markdown, sql, yaml, etc. Using both Eslint and Prettier is highly recommended.

Step 1 – Install Prettier CLI package by npm i -D prettier-eslint-cli

Step 2 – Add a new command to format in package.json"format": "prettier-eslint 'server/**/*.{js,json}' --write"

Just like we did earlier you should now able to run npm run format to format the code using Prettier!

Auto Lint & Format on Git Commit

Even though we’ve built commands to run lint and formatting, most of the time developers forget to run it and commit to git. You can set up npm run lint to your CI/CD so that whenever there are some errors it will fail. However, it will be really nice if we would do these checks every time when someone commits.

Husky and Lint-staged to rescue

Husky allows you to add commands to run before committing. It takes advantage of the git hooks.

Lint-staged – “Run linters against staged git files”. Running Eslint and Prettier on all files on every commit will be very time-consuming. With lint-staged you can run those only on the staged files.

Install husky and lint-staged by npm i -D husky lint-staged

You’ll need to edit the package.json to configure it. Here is the full file:

We tell husky to run lint-staged on every commit. Lint-staged will run eslint, prettier, and ‘git add’ on staged files. The last ‘git add’ is to add the changed filed back to commit since it might be changed formatting.

Need to commit without these checks?

What if there is a fire 🙂 and you try to commit? “Please remove console logs” or something like that? You can tell git not to run these hooks by adding --no-verify (`git commit -m -n “Urgent commit!”`)

Configure VS Code for Eslint and Prettier

Both Eslint and Prettier have great integrations for VS Code. It will automatically highlight errors/warnings, fix code while typing/saving, etc.

Install Eslint and Prettier extensions by ext install dbaeumer.vscode-eslint and ext install esbenp.prettier-vscode

Once you’ve installed the extensions, open VS Code settings.json (Ctrl+,) file and add the following:

Additional Eslint rules for NodeJS

Even though Airbnb’s style guide is more than enough, I’ve put some more extra strict rules. It’s highly recommended

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like