Sub topic 2
Installing and Running ESLint
This chapter gets ESLint into a project and running from the command line. It assumes you are comfortable with modern JavaScript syntax (ES modules, arrow functions), can run Node.js and npm from a terminal, and can edit package.json. The structure of the config file is covered in the next chapter. Here you will write only the smallest config ESLint needs before it will start.
Check Your Node.js Version First
ESLint v9 requires Node.js ^18.18.0, ^20.9.0, or >=21.1.0. On an older version, the install may appear to succeed and then ESLint crashes with a syntax error inside its own code. That error looks like a bug in ESLint, so it can cost you time. Check your version before you install:
node --versionIf the version is out of range, upgrade Node.js with your version manager (nvm, fnm, Volta) before you continue.
Install ESLint Locally, Not Globally
Install ESLint as a development dependency of each project, not with npm install -g. A local install has three advantages:
- Pinned version. Everyone on the team, and the CI server, runs the same ESLint version. That version is recorded in
package.jsonand the lockfile. - Correct plugin resolution. ESLint loads plugins and shared configs through normal
importstatements in your config file. Those imports resolve from the project'snode_modules. A global ESLint paired with local plugins is a common source of "cannot find package" errors. - No hidden setup. A new contributor runs
npm installand has everything they need.
There are two ways to install it.
Option 1: The interactive initializer
npm init @eslint/config@latestThe initializer asks a few questions: what you want to lint, the module system, the framework, whether you use TypeScript, and where the code runs. It then installs the packages it needs and writes a config file, usually eslint.config.mjs or eslint.config.js. This is the fastest start, and it suits new projects.
Option 2: Manual installation
If you want control over exactly what gets added, install the packages yourself:
npm install --save-dev eslint @eslint/js globals| Package | Purpose |
|---|---|
| `eslint` | The core linter and its command-line interface (CLI) |
| `@eslint/js` | ESLint's built-in rule presets, including `recommended` |
| `globals` | Predefined lists of global variables for environments such as Node.js and browsers |
Then create a minimal config file. The .mjs extension makes Node.js treat the file as an ES module, whatever package.json says:
// eslint.config.mjs
import js from "@eslint/js";
export default [js.configs.recommended];The next chapter explains this file line by line. For now, it is enough to know that the config exports an array and that this array turns on ESLint's recommended rules.
Your First Lint Run
Create a file that contains a few deliberate mistakes:
// src/app.js
import { readFile } from "node:fs/promises";
const unused = 42;
export async function load(path) {
const text = await readFile(path, "utf8");
return JSON.parse(txt);
}Run ESLint on the whole project:
npx eslint .npx runs the copy of ESLint in node_modules/.bin, so you use the project's pinned version. The output looks like this:
/home/you/project/src/app.js
4:7 error 'unused' is assigned a value but never used no-unused-vars
7:9 error 'text' is assigned a value but never used no-unused-vars
8:21 error 'txt' is not defined no-undef
✖ 3 problems (3 errors, 0 warnings)Each line gives the position as line and column, then the severity, the message, and the ID of the rule that reported the problem. The rule ID is what you look up in the ESLint docs, what you change in the config, and what you name in inline directives.
The typo txt shows why a linter is worth having. This code parses without trouble and would only fail at runtime, when load is called. ESLint finds the problem without running anything.
What Happens During a Run
Some details of this process matter in practice:
- Config lookup. ESLint searches for the config file starting in the current working directory and moving up through parent directories. If you run it from a subdirectory, it still finds the root config.
- Default file matching. When you pass a directory, ESLint lints
.js,.mjs, and.cjsfiles, plus any other extensions your config'sfilespatterns match. Adding TypeScript means adding patterns, which a later chapter covers. - Default ignores.
node_modules/and.git/are always skipped. Other build output, such asdist/, is linted unless you ignore it in the config. - Parsing. ESLint parses each file into an abstract syntax tree (AST), a tree that represents the structure of the code. Rules inspect the AST, not the raw text. A syntax error stops rules from running on that file and appears as a single fatal error.
Exit Codes and Why They Matter
Scripts, Git hooks, and CI pipelines decide pass or fail from the exit code, not from the printed text.
| Exit code | Meaning |
|---|---|
| `0` | No errors. Warnings may still be present. |
| `1` | At least one error, or more warnings than `--max-warnings` allows |
| `2` | ESLint could not run: missing or invalid config, a plugin that won't load, or an internal crash |
A common mistake is to assume that warnings fail a build. They don't, unless you add --max-warnings 0. Without that flag, warnings pile up quietly over time.
Exit code 2 points to a setup problem, not a code problem. Treat it differently from 1 when you debug a red CI job.
Useful CLI Options
| Option | What it does |
|---|---|
| `npx eslint src/ test/` | Lint only the given directories or files |
| `npx eslint "src/**/*.js"` | Lint files that match a glob. Quote it so the shell doesn't expand it. |
| `--max-warnings 0` | Fail (exit code `1`) if any warning is reported |
| `--quiet` | Report errors only and hide warnings |
| `--format json` | Print machine-readable output for other tools |
| `--output-file report.json` | Write the report to a file instead of stdout |
| `--cache` | Re-lint only files that changed since the last run. State is stored in `.eslintcache`. |
| `--print-config src/app.js` | Show the final merged config ESLint applies to one file |
| `--debug` | Log config loading and file matching details |
Two of these are worth extra attention.
--cache can make repeat runs on large codebases much faster. Add .eslintcache to .gitignore. ESLint invalidates the cache when the config or the ESLint version changes. If you see stale results anyway, delete the cache file.
--print-config is the most useful debugging tool you have. When a rule fires that you thought was off, or stays silent when you expected it to fire, this flag shows the merged config ESLint actually applies to that file.
Common Installation Problems
ESLint couldn't find an eslint.config.(js|mjs|cjs) file: You are running from outside the project, or the config file has a different name, such as.eslintrc.json.SyntaxError: Cannot use import statement outside a module: You named the configeslint.config.js, butpackage.jsondoesn't contain"type": "module". Rename the file to.mjsor add that field.Cannot find package '@eslint/js': The config imports a package that isn't installed locally. Install it as a dev dependency.- Different results locally and in CI: Usually a global ESLint on your machine or a missing lockfile. Run through
npxand commitpackage-lock.json.
Exercise
A CI job runs npx eslint .. The log shows 14 warnings and 0 errors, and the job passes. The team wants any warning to fail the build. What should change, and which exit code will ESLint return once it does?
Show answer
Change the command to npx eslint . --max-warnings 0. With 14 warnings, which is more than the 0 allowed, ESLint exits with code 1 and the job fails. Without the flag, warnings alone always give exit code 0.
Exercise
After you clone a repository and run npx eslint ., ESLint exits with code 2 and reports that it cannot find '@eslint/js'. Is this a problem in the code being linted? What is the likely fix?
Show answer
No. Exit code 2 means ESLint could not run, so no code was linted. The config imports a package that isn't installed, most likely because npm install (or npm ci) hasn't been run yet, or because @eslint/js is missing from devDependencies. Install the dependencies, adding @eslint/js with npm install --save-dev @eslint/js if needed.
Exercise
A rule you believe you turned off still reports problems in src/server/index.js. Which single command shows you the configuration ESLint actually applies to that file?
Show answer
npx eslint --print-config src/server/index.js prints the final merged config for that file, including every rule's effective severity.