Sub topic 2
Installing and Running ESLint
This chapter assumes you can run Node.js and npm from a terminal, edit package.json, and read ES module syntax (import and export). ESLint 9 needs Node.js 18.18 or later. Check your version with node --version.
Installing ESLint in a Project
Install ESLint as a dev dependency, which is a package needed only during development. Each project then pins its own ESLint version, and every contributor runs that same version.
npm install --save-dev eslint @eslint/js globalsThe packages do different jobs:
| Package | Purpose |
|---|---|
| `eslint` | The command-line tool and the linting engine |
| `@eslint/js` | ESLint's built-in rule sets, including `recommended` |
| `globals` | Lists of predefined global variables for environments such as Node.js and the browser |
ESLint also has an interactive setup command, npm init @eslint/config@latest. It asks about your project and writes the config for you. Installing by hand, as above, shows you exactly what goes into the project.
A Minimal Config
ESLint 9 will not run without a config file. Create eslint.config.mjs in the project root. The .mjs extension tells Node.js to treat the file as an ES module. If your package.json contains "type": "module", you can name it eslint.config.js instead.
// eslint.config.mjs
import js from "@eslint/js";
import globals from "globals";
export default [
js.configs.recommended,
{ languageOptions: { globals: { ...globals.node } } },
];The next chapter explains this file line by line. For now, note that it turns on the recommended rules and tells ESLint that Node.js globals such as console exist.
Running ESLint
Create src/app.js with two deliberate mistakes:
const greeting = "hello";
console.log(mesage);Lint the whole project, or point ESLint at a single file:
npx eslint .
npx eslint src/app.jsThe output lists each problem with its line, column, severity, message, and the ID of the rule that reported it:
/home/me/demo/src/app.js
1:7 error 'greeting' is assigned a value but never used no-unused-vars
2:13 error 'mesage' is not defined no-undef
✖ 2 problems (2 errors, 0 warnings)The rule ID is the most useful part of each line. You search for it in the documentation, and you use it when you change a rule's settings.
Exit Codes and Common Errors
Scripts and CI systems read ESLint's exit code. Code 0 means no errors were reported. Code 1 means at least one error was reported. Code 2 means ESLint itself failed, for example because of a broken config. Warnings alone still produce 0. Add --max-warnings 0 if you want warnings to fail the run as well.
Exercise
You fix both errors in src/app.js, but one rule is still set to "warn" and reports a warning. What exit code does npx eslint . return, and how would you make that warning fail the run?
Show answer
It returns 0, because warnings alone do not fail ESLint. Run npx eslint . --max-warnings 0 to make any warning produce exit code 1.
Exercise
A teammate runs npx eslint . and gets exit code 2 with "Could not find config file", but the repository contains .eslintrc.json. What is wrong?
Show answer
ESLint 9 reads only flat config files (eslint.config.js, .mjs, or .cjs) and ignores .eslintrc.json. The project needs an eslint.config file, either written by hand or migrated from the old one.