Installing and Running ESLint

A detailed guide to installing and running ESLint so you can start checking your code for errors and style issues.

🎯 Introduction

Learning to install and run ESLint lets you detect potential errors and style issues in JavaScript, improve code quality, and collaborate more effectively. This beginner-friendly guide walks through installation and basic usage step by step.


🔧 Prerequisites for Installing ESLint

First, make sure Node.js is installed. Run the following commands in your terminal:

node -v
npm -v

If both commands print version numbers, Node.js and npm are installed successfully.

Node.js is a JavaScript runtime, and npm is the package manager included with it. You need both to install ESLint.

If they are not installed, download and install the latest stable version from the Node.js website.


🛠 Installing ESLint Step by Step

1. Create a Project Folder (Optional)

Skip this step if you already have a project. To create a new one:

mkdir my-eslint-project
cd my-eslint-project

2. Initialize the Project (Generate package.json)

npm init -y

This command creates a package.json file with default settings to manage project dependencies.


3. Install ESLint

Install ESLint as a development dependency in your project:

npm install eslint --save-dev
The --save-dev flag marks ESLint as a development dependency, so it does not need to be included with production dependencies.

4. Initialize the ESLint Configuration

After installation, run this command to generate an ESLint configuration file:

npx eslint --init

You will see a series of prompts. The following examples show suggested choices (✅ indicates your selection):

  1. Which files do you want to lint?
? What do you want to lint? …
✔ JavaScript
  JSON
  JSON with comments
  JSON5
  Markdown
  CSS
  1. How would you like to use ESLint?
? How would you like to use ESLint? …
  To check syntax only
✔ To check syntax and find problems
  1. What module system does your project use?
? What type of modules does your project use? …
✔ JavaScript modules (import/export)
  CommonJS (require/exports)
  None of these
  1. Which framework does your project use?
? Which framework does your project use? …
  React
  Vue.js
✔ None of these
  1. Does your project use TypeScript?
? Does your project use TypeScript? › No
  1. Where does your code run?
? Where does your code run? …  (Press <space> to select, <a> to toggle all, <i> to invert selection)
✔ Browser
  Node
  1. Would you like to install the required dependencies now?
The config that you've selected requires the following dependencies:

eslint, @eslint/js, globals
? Would you like to install them now? › No

Choose the options that match your project.

Once setup is complete, an eslint.config.mjs file is created in the project root.


5. Example ESLint Configuration

An example eslint.config.mjs file:

// eslint.config.js (flat config, recommended since ESLint v9)
import js from '@eslint/js';
import globals from 'globals';
import { defineConfig } from 'eslint/config';

export default defineConfig([
    {
        // Match JavaScript files (.js, .mjs, and .cjs); this configuration applies only to these files.
        files: ['**/*.{js,mjs,cjs}'],

        // Register the official @eslint/js plugin, which provides built-in JavaScript rules.
        plugins: { js },

        // Apply the recommended rules from @eslint/js for common issues and best practices.
        extends: ['js/recommended'],

        // Enable browser globals such as window, document, and console to avoid undefined-variable errors.
        languageOptions: { globals: globals.browser },

        // Use rules to control linting behavior. This example disallows semicolons.
        rules: {
            semi: ['error', 'never'],
        },
    },
]);
This is the newer configuration format, which allows different rules for different file types.

▶️ Running ESLint

1. Run ESLint from the Command Line

Suppose your source files are in a src folder. Run:

npx eslint src

ESLint checks all supported files in src and reports errors and warnings.


2. Understanding the Output

Suppose src/index.js contains:

const name = 'John';

console.log(nam);

Running ESLint produces output similar to this:

src/index.js
  1:7   error  'name' is assigned a value but never used  no-unused-vars
  3:13  error  'nam' is not defined                       no-undef

✖ 2 problems (2 errors, 0 warnings)

The first error says that name on line 1 is assigned a value but never used.

The second error says that nam on line 3 is not defined.


3. Apply Automatic Fixes

ESLint provides automatic fixes. Use the --fix flag to fix some formatting issues:

npx eslint src --fix

Depending on your configured rules, it can add missing semicolons, adjust indentation, and more.

The --fix flag is commonly used for formatting fixes. It cannot fix every issue ESLint reports.

4. Add package.json Scripts

For convenience, add scripts to package.json:

"scripts": {
  "lint": "eslint src",
  "lint:fix": "eslint src --fix"
}

You can then run:

npm run lint

or

npm run lint:fix

to check your code or apply fixes, respectively.


🔌 Integrating ESLint with Your Editor

Install an ESLint extension for your editor to catch potential issues as you write code.

For example, installing the ESLint extension in Cursor lets the editor check your code as you work and report errors when you save, helping you fix issues promptly and follow your coding conventions.


❓ Questions and Answers

Q1: Why run eslint --init after installing ESLint?

It generates a configuration file, .eslintrc in the legacy configuration format, that tells ESLint which rules and environments to use.

Q2: When should I use ESLint's --fix flag?

Use it to automatically correct formatting and some simple issues, saving time on manual edits.


🧪 Practice

Use the following template to install, initialize, and run ESLint, then see what it reports.

mkdir demo-eslint
cd demo-eslint
npm init -y
npm install eslint --save-dev
npx eslint --init

Next, create src/test.js with the following content:

let a = 10
console.log(a)

Run ESLint, check whether your configured rules report missing semicolons, and try applying automatic fixes with --fix.


🎉 You can now install, initialize, and run ESLint! Start using it to write more consistent and reliable JavaScript.