ESLint and Prettier Setup with Git Pre-commit hook🎣

Search for a command to run...

Do you have to do this for every project you create?
When I started learning React some time ago, I used this approach at first but I always had to instal Eslint and Prettier for each project. I had to stop at some point and stick to using create-react-app. I'd really love to know if there's a way to install it just once and ensure its automatically added to every other project I create in the future.
The Problem That Makes Developers Pull Their Hair Out When you design infrastructure across multiple AWS regions, you expect each region to be self-contained — its own EKS clusters, databases, Lambdas, and certificates — all neatly separated by regio...
In my experience building distributed systems, I've consistently seen the need to evolve messaging infrastructure as applications mature. What starts as a simple Redis pub/sub for prototyping might need to scale to Kafka for high throughput, or switc...

Introduction: In the fast-paced world of backend development, working with PostgreSQL comes with its own set of hurdles. In this blog post, I'll share my experiences facing challenges like index and table bloating, an unusual performance bug related ...

Greetings fellow engineers, As we bid farewell to another year filled with coding, debugging, and pushing the boundaries of what's possible, it's time to reflect on the pages that have molded our minds in 2023 and prepare for the adventures that 2024...

Introduction Hey there, fellow developers! Today, I want to share with you a powerful technique that can take your NestJS applications to a whole new level. It's called dependency injection (DI), and trust me, once you get the hang of it, it'll becom...

In this post, we will look at how to integrate ESLint and Prettier into your vs-code setup and project in order to ensure cleaner code and consistent across the team. We might not be perfect all the time, as developers sometimes we may push code in a hurry and miss like a closing bracket, declaring a variable, or importing a function. so we will also look at how to ensure code integrity and validity on every commit you make by watching for errors and warnings in your project code by ESlint and Prettier. First, let us look at what are ESLint and Prettier
ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs. ESLINT
Prettier is very popular because it improves code readability and makes the coding style consistent for teams. Developers are more likely to adopt a standard rather than writing their own code style from scratch, so tools like Prettier will make your code look good without you ever having to dabble in the formatting.Prettier
Make sure you installed the vs code extensions for Prettier and ESLint .
Make sure you are inside a node project or create a package.json file by running the npm init command and initialize the git by running git init.
npm install -D eslint prettier
npx install-peerdeps --dev eslint-config-airbnb
npm install -D eslint-config-prettier eslint-plugin-prettier husky lint-staged
Create a file by name .eslintrc.json and copy the below lines into the file
{
"extends": ["airbnb", "prettier"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": ["error"]
},
}
Create a file by name .prettierrc and copy the below lines into the file
{
"printWidth": 100,
"singleQuote": true
}
to quickly format your code you can enable formatOnSave on vs-code which enables formatting when you press CTRL + S. You can enable this in vs-code settings, you can open the setting by CTRL +, and search for formatonsave and enable it.

In package.json on the same level as dependencies paste the following lines
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
]
}
you can skip the pre-commit check while committing if you don't need for a single commit by providing the --no-verify flag
git commit -m "no check" --no-verify
create an index.js file and copy the contents
const fs = require('fs');
const os = require('os');
ESLint warns you about the unused variables, as a push first, fix after developer we are gonna commit the files by
git add .
git commit -m "test"
Now if everything is working correct you will see this error
index.js
1:7 error 'fs' is assigned a value but never used no-unused-vars
2:7 error 'os' is assigned a value but never used no-unused-vars
✖ 2 problems (2 errors, 0 warnings)
husky > pre-commit hook failed (add --no-verify to bypass)