Sub topic 3
Understanding eslint.config.js
This chapter assumes you already have ESLint installed in a project and can run npx eslint . from the command line. It also assumes you can read ES module syntax (import, export default) and are comfortable editing package.json. Choosing individual rules and severities gets its own chapter, as do shared configs. Here the focus is on how the configuration file is structured and how ESLint turns it into the settings for one specific file.
The config file is a JavaScript module
ESLint 9 and later look for a flat config file in the current working directory, then in each parent directory, until they find one. The recognized names are:
| File name | Module format | Notes |
|---|---|---|
| `eslint.config.js` | Depends on `"type"` in `package.json` | ESM if `"type": "module"`, otherwise CommonJS |
| `eslint.config.mjs` | Always ESM | Safe choice when you don't control `"type"` |
| `eslint.config.cjs` | Always CommonJS | Use `module.exports = [...]` |
| `eslint.config.ts` / `.mts` / `.cts` | TypeScript | Needs a TypeScript loader such as `jiti` installed |
Because the file is ordinary JavaScript, you can import packages, compute values, and reuse variables. ESLint evaluates the module and reads its default export. That export must be an array of config objects, or a value that resolves to one.
The working model behind flat config fits in one sentence: the array is a list of layers, each layer says which files it applies to, and ESLint merges every matching layer in order, with later layers overriding earlier ones.
Anatomy of a config object
Each element of the array is a plain object. The most commonly used keys are below.
| Key | Purpose |
|---|---|
| `files` | Glob patterns this object applies to. If omitted, it applies to every linted file. |
| `ignores` | Glob patterns to exclude from this object. If it is the *only* key, it becomes a global ignore. |
| `languageOptions` | `ecmaVersion`, `sourceType`, `globals`, `parser`, `parserOptions`. |
| `linterOptions` | `noInlineConfig`, `reportUnusedDisableDirectives`. |
| `plugins` | An object mapping a namespace to a plugin object. |
| `rules` | Rule names mapped to severity and options. |
| `settings` | Shared data that any rule can read. |
| `name` | A label shown in debugging tools. |
Here is a complete, runnable config for a project with Node.js source code, browser code, and tests. It needs @eslint/js and globals installed as dev dependencies.
// eslint.config.mjs
import js from "@eslint/js";
import globals from "globals";
export default [
// 1. Global ignores: an object with only "ignores"
{
name: "project/ignores",
ignores: ["dist/", "coverage/", "**/*.min.js"],
},
// 2. Baseline rules for every linted file
js.configs.recommended,
// 3. Settings shared by all JavaScript files
{
name: "project/base",
files: ["**/*.{js,mjs,cjs}"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
linterOptions: {
reportUnusedDisableDirectives: "error",
},
},
// 4. Server code runs in Node.js
{
name: "project/server",
files: ["server/**/*.js"],
languageOptions: {
globals: { ...globals.node },
},
},
// 5. Client code runs in the browser
{
name: "project/client",
files: ["client/**/*.js"],
languageOptions: {
globals: { ...globals.browser },
},
},
// 6. CommonJS files
{
name: "project/commonjs",
files: ["**/*.cjs"],
languageOptions: {
sourceType: "commonjs",
globals: { ...globals.node },
},
},
// 7. Tests: relax one rule, add test globals
{
name: "project/tests",
files: ["**/*.test.js"],
languageOptions: {
globals: { ...globals.node, ...globals.jest },
},
rules: {
"no-unused-expressions": "off",
},
},
];How ESLint computes the config for one file
When ESLint lints server/db.test.js, it does not use the whole array. It builds a calculated config for that file path:
- It checks the global ignores. If the path matches, the file is skipped entirely.
- It decides whether the file should be linted at all. The file must match the default patterns (
**/*.js,**/*.mjs,**/*.cjs) or afilespattern in some config object. - It walks the array from top to bottom and keeps each object whose
filesmatch the path and whoseignoresdo not. - It merges the kept objects in order. Later values win for the same key.
For server/db.test.js, objects 2, 3, 4, and 7 match. The merge works per key, not per object:
rulesmerge by rule name.js.configs.recommendedturns onno-unused-vars, and nothing later touches it, so it stays on. Object 7 switches offno-unused-expressions.languageOptionsmerge by property.sourceType: "module"comes from object 3.globalsfrom object 4 are combined with those from object 7.- If a rule appears twice with options, the later entry replaces the earlier one. If the later entry gives only a severity, such as
"warn", the earlier options are kept and only the severity changes.
`files` and `ignores` in detail
Glob patterns are resolved relative to the directory that contains the config file, not the directory you run ESLint from. A few behaviors catch people out:
fileswithoutignores,ignoreswithoutfiles. An object withignoresplus other keys, such asrules, is a local exclusion. It only stops that object from applying. Only an object whose sole key isignores(optionally withname) removes files from linting altogether.- Directory patterns.
"dist/"with a trailing slash ignores the directory."dist/**"ignores the files inside it but still lets ESLint descend into it, which matters when you want to un-ignore something with a!negation such as"!dist/keep.js". - Default ignores.
**/node_modules/and.git/are always ignored. The old.eslintignorefile is not read in flat config. - Non-JavaScript extensions. Adding
files: ["**/*.jsx"]to an object is what makes ESLint lint.jsxfiles at all. Without such a pattern,npx eslint .skips them.
ESLint 9.22 and later also export helpers that make intent explicit:
// eslint.config.mjs
import { defineConfig, globalIgnores } from "eslint/config";
import js from "@eslint/js";
export default defineConfig([
globalIgnores(["dist/", "coverage/"]),
{
files: ["**/*.js"],
extends: [js.configs.recommended],
rules: { eqeqeq: ["error", "always"] },
},
]);defineConfig gives editors type hints and flattens nested arrays. Its extends key applies the listed configs only to that object's files. That scoping is often what you want when a shared config would otherwise apply to every file type.
languageOptions defaults
If you set nothing, ESLint uses ecmaVersion: "latest". It uses sourceType: "module" for .js and .mjs files and "commonjs" for .cjs files. No environment globals are defined by default. That is the most common cause of 'process' is not defined or 'window' is not defined errors from no-undef. Flat config has no env key. Globals come from the globals package, spread into languageOptions.globals. You can also declare individual names as "readonly" or "writable":
// eslint.config.mjs
export default [
{
languageOptions: {
globals: { __APP_VERSION__: "readonly" },
},
},
];Inspecting the result
When a rule fires unexpectedly, look at the calculated config rather than guessing:
npx eslint --print-config server/db.test.js
npx eslint --inspect-config--print-config prints the merged JSON for one file. --inspect-config starts a local web UI that shows each config object, its name, and which files it matches. The name keys in the example above exist for this purpose.
Common errors
| Symptom | Likely cause |
|---|---|
| `SyntaxError: Cannot use import statement outside a module` | `eslint.config.js` uses `import` but `package.json` lacks `"type": "module"`. Rename it to `.mjs`. |
| Ignored folder still linted | `ignores` sits in an object that also has `rules` or `files`, so it is local, not global. |
| Override has no effect | The override object comes before a broader object that sets the same rule. |
| `no-undef` on `require` or `document` | Globals missing, or `sourceType` wrong for `.cjs` files. |
| `.ts` or `.jsx` files skipped | No `files` pattern includes that extension. |
Exercise
A config array is [{ files: ["src/**/*.js"], rules: { "no-console": "error" } }, { ignores: ["src/legacy/**"], rules: { "no-console": "off" } }]. What severity does no-console have for src/app.js and for src/legacy/old.js, and is src/legacy/old.js linted?
Show answer
For src/app.js, both objects match. The second has no files and its ignores do not match, so it merges last and no-console is "off". For src/legacy/old.js, the second object is excluded by its own ignores, so only the first applies and no-console is "error". The file is still linted because that ignores is local: the object also has rules, so it is not a global ignore.
Exercise
Your project has "type": "commonjs" in package.json, and you want an ESM config file that defines browser globals only for files under public/. Name the file and write the config object.
Show answer
Name it eslint.config.mjs so it is always treated as ESM. Inside, import globals from "globals" and export an array containing { files: ["public/**/*.js"], languageOptions: { globals: { ...globals.browser } } }, placed after any baseline configs.
Exercise
Which command shows the exact merged rules and language options that ESLint will use for client/main.js?
Show answer
npx eslint --print-config client/main.js. For a visual view of every config object and the files it matches, run npx eslint --inspect-config.