Sub topic 3
Understanding eslint.config.js
This chapter assumes you can read ES module syntax (import and export default) and that ESLint is already installed in your project. Here you will learn how ESLint reads its flat config file and how it decides which settings apply to each file it lints.
The config is an array
eslint.config.js exports an array of config objects. ESLint walks through the array in order for every file it lints. Each object whose files pattern matches that file gets merged in. When two objects set the same thing, the later one wins.
// eslint.config.js (requires "type": "module" in package.json, or name it eslint.config.mjs)
import js from "@eslint/js";
import globals from "globals";
export default [
js.configs.recommended,
{
files: ["**/*.js"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
globals: { ...globals.node },
},
rules: {
"no-unused-vars": "warn",
eqeqeq: ["error", "always"],
},
},
];The first entry, js.configs.recommended, has no files key, so it applies to every file ESLint lints. The second entry applies only to files matching **/*.js. It sets no-unused-vars to "warn", which overrides the "error" severity that the recommended set uses.
Keys you will use most
| Key | Purpose |
|---|---|
| `files` | Glob patterns that select which files this object applies to |
| `ignores` | Glob patterns to exclude from this object, or from all linting |
| `languageOptions` | Parser settings, `ecmaVersion`, `sourceType`, and `globals` |
| `rules` | Rule names mapped to severity and options |
| `plugins` | Extra rules provided by packages, keyed by a namespace |
The globals package supplies ready-made lists of variable names such as process and __dirname for Node.js, or window for browsers. If you don't declare these globals, the no-undef rule reports them as undefined variables.
Global ignores
An object that contains only ignores works differently from other objects. It removes the matched paths from linting entirely:
export default [
{ ignores: ["dist/**", "coverage/**"] },
// ...other config objects
];If you add any other key to that object, ignores only excludes files from that one object.
Common errors
- "Cannot use import statement outside a module": your
package.jsonlacks"type": "module". Rename the file toeslint.config.mjsor add that field. - Override ignored: an override placed before a broader object gets overwritten by it. Put specific overrides last.
- Files not linted: by default ESLint lints
**/*.js,**/*.mjs, and**/*.cjs. Other extensions need a matchingfilesentry.
Exercise
Your config array is [{ files: ["test/**/*.js"], rules: { "no-unused-vars": "off" } }, js.configs.recommended]. Unused variables in test files are still reported as errors. Why, and how do you fix it?
Show answer
js.configs.recommended comes later in the array and applies to all files, so its "no-unused-vars": "error" overrides the test-specific "off". Move the test override object after js.configs.recommended.
Exercise
You add { ignores: ["build/**"], rules: { eqeqeq: "off" } } to your config, but ESLint still lints files in build/. What went wrong?
Show answer
Because the object has a key other than ignores, its ignores only excludes build/ from that one object. To ignore build/ globally, put { ignores: ["build/**"] } in its own object with no other keys.