DEV Community

Cover image for Automatically Format your code on Git Commit using Husky, ESLint, Prettier in 9 minutes
Tapajyoti Bose
Tapajyoti Bose

Posted on • Edited on • Originally published at tapajyoti-bose.Medium

Automatically Format your code on Git Commit using Husky, ESLint, Prettier in 9 minutes

When collaborating on a project with several other developers, maintaining a consistent code style drastically improves the code readability and maintainability.

Luckily we can automate this crucial process using Husky, ESLint, Prettier to make sure the code is formatted, every time someone commits.

1. Install Packages

We need to install the following packages:

  • Husky: A tool that makes working with git hooks a piece of cake
  • ESLint: Linter for JavaScript
  • Prettier: Code formatter
  • Lint-staged: As the docs state: Run linters against staged git files and don't let ๐Ÿ’ฉ slip into your codebase!

To install the packages, use:



npm install --save-dev eslint prettier lint-staged husky


Enter fullscreen mode Exit fullscreen mode

2. Configure ESLint

Run the following command to initialize ESLint:



npx eslint --init


Enter fullscreen mode Exit fullscreen mode

You will be prompted to answer a few questions, from which the configuration for your specific use case will be generated

eslint setup

The generated configuration would look something like this:



{
  "env": {
    "browser": true,
    "es2021": true
  },
  "extends": ["eslint:recommended"]
}


Enter fullscreen mode Exit fullscreen mode

If you are lazy like me, just copy and paste the configuration into a file called .eslintrc.json instead of answering the long list of questions.

To check out all the available configurations, visit the ESLint documentation. The config provided above is just a barebone example.

3. Configure Prettier

Configuring Prettier is similar to ESlint, just add a .prettierrc.json file to your project root and you are good to go.

You can use the following configuration as a starting point:



{
  "bracketSpacing": true,
  "semi": true,
  "singleQuote": true,
  "trailingComma": "all",
  "printWidth": 80,
  "tabWidth": 2
}


Enter fullscreen mode Exit fullscreen mode

To check out all the available options, head over to the Prettier Documentation.

Also add a .prettierignore file to your project root to ignore files that you don't want to be formatted, use the following content as a base:



package-lock.json
yarn.lock
node_modules
# any other unwanted files or folders


Enter fullscreen mode Exit fullscreen mode

4. Configure Lint-staged

Lint-staged too is quite simple to configure. Just add the following to your package.json file and you are good to go:



{
  /* other configurations */
  "lint-staged": {
    "**/*.{js,jsx,json}": ["eslint . --fix", "prettier --write ."]
  }
}


Enter fullscreen mode Exit fullscreen mode

5. Configure Husky

We are at the last peg of our configuration journey. Configuring Husky is a bit trickier than the other configurations. Add the following script to your package.json file:



{
  /* other configurations */
  "scripts": {
    /* other scripts */
    "configure-husky": "npx husky install && npx husky add .husky/pre-commit \"npx --no-install lint-staged\""
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  }
}


Enter fullscreen mode Exit fullscreen mode

Run the configure-husky script to install Husky and connect it to the pre-commit hook:



npm run configure-husky


Enter fullscreen mode Exit fullscreen mode

You are now set! Let's try committing some changes

committing changes

BINGO! IT WORKS! ๐ŸŽ‰

From now on, maintaining a consistent code style in your project will be a breeze.

Happy Developing!

Finding personal finance too intimidating? Checkout my Instagram to become a Dollar Ninja

Thanks for reading

Need a Top Rated Front-End Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

I am a freelancer who will start off as a Digital Nomad in mid-2022. Want to catch the journey? Follow me on Instagram

Follow my blogs for Weekly new Tidbits on Dev

FAQ

These are a few commonly asked questions I get. So, I hope this FAQ section solves your issues.

  1. I am a beginner, how should I learn Front-End Web Dev?
    Look into the following articles:

    1. Front End Development Roadmap
    2. Front End Project Ideas

Top comments (4)

Collapse
 
darkwiiplayer profile image
๐’ŽWii ๐Ÿณ๏ธโ€โšง๏ธ

Tried something similar using git hooks once, but it's a bit more complicated to set up ๐Ÿ˜…

Collapse
 
aviavinav profile image
Avi Avinav

Amazing article! I was wanting to implement this in my projects, didn't know how to and then I found this.

Collapse
 
visraelfs profile image
visraelfs

I want to know if is there a way to remove prettier from husky git pre-commit?, because I don't want prettier add a new line at end of file in my classes.

Image description

Collapse
 
ruppysuppy profile image
Tapajyoti Bose

If you don't want an extra line, configure prettier for that. If you don't want prettier to run, configure lint staged, decide which one you want & update accordingly