Sub topic 18
Migrating from .eslintrc to Flat Config
This chapter assumes you have read the earlier chapters on eslint.config.js, rules, and shared configs. You should also be comfortable reading JSON and ES module imports.
ESLint 9 uses flat config by default. A project that still has .eslintrc.json or .eslintrc.js will fail to lint until you migrate it. You can set ESLINT_USE_FLAT_CONFIG=false to get the old behavior back for a while, but that is only a stopgap, because the legacy format is deprecated.
How Legacy Keys Map to Flat Config
Most legacy keys have a direct replacement. The table below lists the ones you will hit most often.
| Legacy `.eslintrc` | Flat config `eslint.config.js` |
|---|---|
| `extends: ["eslint:recommended"]` | Import the config and put it in the array: `js.configs.recommended` |
| `env: { node: true }` | `languageOptions.globals` with `globals.node` |
| `parserOptions.ecmaVersion` / `sourceType` | `languageOptions.ecmaVersion` / `sourceType` |
| `parser: "some-parser"` (string) | `languageOptions.parser` (an imported object) |
| `plugins: ["foo"]` (strings) | `plugins: { foo: fooPlugin }` (imported objects) |
| `overrides: [{ files, rules }]` | Another object in the array with its own `files` |
| `.eslintignore` file | An object containing only `ignores` |
The main change is that flat config contains no string lookups. Every config, parser, and plugin is imported as a JavaScript value, so a missing package fails at import time with a clear error.
A Worked Migration
Here is a typical legacy file:
{
"env": { "node": true, "es2022": true },
"extends": ["eslint:recommended"],
"parserOptions": { "sourceType": "module" },
"rules": { "eqeqeq": "error" },
"overrides": [
{ "files": ["test/**/*.js"], "env": { "mocha": true } }
]
}Its flat equivalent, with the contents of .eslintignore folded in:
// eslint.config.mjs
import js from "@eslint/js";
import globals from "globals";
export default [
{ ignores: ["dist/", "coverage/"] },
js.configs.recommended,
{
files: ["**/*.js"],
languageOptions: {
ecmaVersion: 2022,
sourceType: "module",
globals: { ...globals.node },
},
rules: { eqeqeq: "error" },
},
{
files: ["test/**/*.js"],
languageOptions: { globals: { ...globals.mocha } },
},
];Install the two imported packages first with npm install --save-dev @eslint/js globals. When you are done, delete .eslintrc.json and .eslintignore so nobody edits the wrong file.
Letting a Tool Do the First Pass
ESLint publishes a migrator that reads your old file and writes eslint.config.mjs:
npx @eslint/migrate-config .eslintrc.jsonReview the output before you commit it. If a shareable config you depend on has not shipped a flat version yet, the migrator wraps it in FlatCompat from @eslint/eslintrc. That compatibility layer works, but treat it as temporary and replace it once the package supports flat config.
Checking the Result
Run npx eslint --print-config src/index.js and compare the output with what the old setup produced. Then run npx eslint . and look for new or missing warnings. Flat config lints .js, .mjs, and .cjs files by default. Any other extension, such as .jsx, needs an explicit files pattern.
Exercise
A legacy config has "plugins": ["import"] and "rules": { "import/no-unresolved": "error" }. How do you express the plugin part in flat config?
Show answer
Import the plugin (import importPlugin from "eslint-plugin-import";) and register it as an object: plugins: { import: importPlugin }. The rule name "import/no-unresolved" stays the same, because the key you choose in the plugins object sets the rule prefix.
Exercise
After migrating, ESLint still lints files in dist/. Your config has { files: ["*/.js"], ignores: ["dist/"], rules: {...} }. Why, and how do you fix it?
Show answer
Because that object has other keys besides ignores, the ignore applies only to that object and is not global. Move it into its own object, { ignores: ["dist/"] }, near the top of the array.