Member-only story
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 stagedgit
files and don't let 💩 slip into your codebase!
To install the packages, use:
npm install --save-dev eslint prettier lint-staged husky
2. Configure ESLint
Run the following command to initialize ESLint:
npx eslint --init
You will be prompted to answer a few questions, from which the configuration for your specific use case will be generated
The generated configuration would look something like this:
{
"env": {
"browser": true,
"es2021": true
},
"extends": ["eslint:recommended"]
}
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
}
To check out all the available options, head over to the Prettier Documentation.