Skip to content

Sagar Kunwar

Menu
  • Home
  • YouTube
  • Blog
  • Contact
  • Privacy Policy
Menu

Setting Up ESLint and Prettier for Clean Code

Posted on June 17, 2025June 17, 2025 by Sagar Kunwar

Setting Up ESLint and Prettier for Clean Code

Hey there, code crafters! If you’re stepping into the world of coding with JavaScript or planning your next big web project, you’ve probably heard about the magic of clean code. Clean code isn’t just about making it look pretty; it’s about making it easier to maintain, read, and improve. This is where ESLint and Prettier come to the rescue, acting like your friendly neighborhood guardians of code.

In this tutorial, let’s walk through how to set up ESLint and Prettier in your project. No worries if you’re new to this; I’ll guide you every step of the way!

Why ESLint and Prettier?

Before we dive in, let’s quickly understand why we need these tools:

– ESLint: This is a powerful tool designed to catch bugs and enforce coding conventions. It helps you stick to best practices and avoid common pitfalls in JavaScript.

– Prettier: Whereas ESLint focuses on catching errors, Prettier will take care of formatting. It enforces a consistent style by automatically formatting your code.

Using both together can transform a messy code base into something that is clean and delightful to read.

Step 1: Initialize Your Project

If you haven’t created a project yet, let’s get that set up quickly:

1. Open your terminal or command prompt.
2. Create a new directory for your project:

bash
mkdir my-awesome-project

3. Navigate into your project directory:

bash
cd my-awesome-project

4. Initialize npm (Node Package Manager):

bash
npm init -y

Your project is now initialized with a package.json file. Easy, right?

Step 2: Install ESLint

Now, let’s install ESLint into your project:

1. Install ESLint using npm:

bash
npm install eslint --save-dev

2. Next, initialize ESLint to create a configuration file:

bash
npx eslint --init

During the initialization, you’ll be asked several questions:

– How would you like to use ESLint? (Pick an option that suits your need; for beginners, “To check syntax and find problems” is a good start).
– What type of modules does your project use? (Say “JavaScript modules (import/export)” if you’re unsure).
– Which framework does your project use? (You can select “None of these” if you’re in doubt).
– Does your project use TypeScript? (Answer accordingly).
– Where does your code run? (Choose between “Browser” or “Node” based on your environment).
– What format do you want your config file to be in? (I’d recommend JSON for its simplicity).

Boom! ESLint is now set up with a configuration file called .eslintrc.json.

Step 3: Install Prettier

On to Prettier:

1. Install Prettier:

bash
npm install prettier --save-dev

2. Create a configuration file for Prettier:
– Make a new file called .prettierrc in your project’s root directory.
– Add your custom formatting rules in JSON format. Here’s a simple example:

json
{
"semi": true,
"singleQuote": true
}

Oops! Did you see the magic? Prettier will now format your code with semicolons and single quotes everywhere. Feel free to tweak these rules to your liking!

Step 4: Combine the Powers of ESLint and Prettier

To make ESLint and Prettier play nicely together, follow these steps:

1. Install the necessary packages:

bash
npm install eslint-config-prettier eslint-plugin-prettier --save-dev

2. Update your .eslintrc.json to integrate Prettier:

json
{
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}

Adding plugin:prettier/recommended to the extends array gives you Prettier’s linting as an ESLint rule, which will mark formatting issues as errors.

Step 5: Using ESLint and Prettier

You’re now ready to use these tools to keep your code clean! Here’s how:

– Lint your code with ESLint:

bash
npx eslint

This will check your code against all the rules you’ve set up.

– Format your code with Prettier:

bash
npx prettier --write

This command will reformat your code file.

For added convenience, consider adding script commands to your package.json. Here’s a quick add:

json
"scripts": {
"lint": "eslint .",
"format": "prettier --write ."
}

Now, simply run npm run lint to check your entire project for linting errors, and npm run format to format all files.

Wrapping Up

And there you have it! You’ve effectively set up ESLint and Prettier, two incredible allies for keeping your code clean and maintainable. Not only will this make your codebase a pleasure to work with, but it will also prepare you for collaborating with other developers by adhering to standardized formats.

Optional Practice Ideas

– Experiment with Rules: Play around with different ESLint and Prettier rules to see their effects.
– Continuous Integration: Integrate your setup with CI/CD pipelines to automate checks on every code push.
– Pre-commit Hooks: Implement tools like Husky to automatically lint and format before every commit.

Happy coding, and may your code always be clean and sparkly! 🎉

(Visited 21 times, 1 visits today)

Leave a Reply Cancel reply

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

Recent Posts

  • Guide to Responsive Images with `srcset`
  • # Creating and Validating Forms in React with Formik
  • Setting Up Multiple Pages with React Router DOM v6
  • How to Use VS Code Like a Pro: Extensions and Shortcuts

Categories

  • Blog
  • Javascript
  • PHP
  • Support
  • Uncategorized
  • Web Hosting
June 2025
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930  
« May   Jul »
© 2025 Sagar Kunwar | Powered by Superbs Personal Blog theme