Sub topic 18
Migrating from .eslintrc to Flat Config
Older projects, and many tutorials online, configure ESLint with .eslintrc.* files. ESLint v9 made flat config (eslint.config.js) the default and deprecated the eslintrc format, and ESLint v10 removes it. This chapter shows how each legacy key maps to flat config. It also covers using the official migrator, wrapping plugins that are not ready for flat config, and checking that the migrated config behaves the same as the old one.
This chapter assumes you have read the earlier chapters on eslint.config.js, rules and severity, and shared configs. You should be comfortable with ES module import syntax and running npx commands.
What changes conceptually
The eslintrc system found config files by walking up the directory tree. It merged every .eslintrc it found until it reached one marked root: true. It loaded plugins and shared configs by name, as strings, and ESLint resolved those names itself. Flat config drops all of that:
- A project has one config file, and it exports an array of config objects.
- Objects later in the array override earlier ones for files they match.
- Plugins, parsers, and shared configs are JavaScript values you
import. ESLint no longer resolves them from strings. filesandignorespatterns on each object replace bothoverridesand--ext.
Key-by-key mapping
| eslintrc | Flat config |
|---|---|
| `extends: ["eslint:recommended"]` | `js.configs.recommended` from `@eslint/js` as an array entry |
| `extends: ["prettier"]` | Imported `eslint-config-prettier` as an array entry |
| `env: { node: true }` | `languageOptions.globals: { ...globals.node }` |
| `parserOptions.ecmaVersion` / `sourceType` | `languageOptions.ecmaVersion` / `sourceType` |
| `parser: "@typescript-eslint/parser"` | `languageOptions.parser: importedParser` |
| `plugins: ["import"]` | `plugins: { import: importPlugin }` |
| `overrides: [{ files, ... }]` | Another array object with `files` |
| `.eslintignore` | `{ ignores: [...] }` with no other keys |
| `root: true` | Delete it |
| `--ext .ts,.jsx` | `files: ["**/*.ts", "**/*.jsx"]` |
| `noInlineConfig`, `reportUnusedDisableDirectives` | `linterOptions.noInlineConfig`, `linterOptions.reportUnusedDisableDirectives` |
A worked migration
Here is a typical legacy setup. The .eslintrc.json file:
{
"root": true,
"env": { "node": true, "es2022": true },
"parserOptions": { "ecmaVersion": "latest", "sourceType": "module" },
"extends": ["eslint:recommended", "prettier"],
"rules": {
"no-unused-vars": "warn",
"eqeqeq": ["error", "always"]
},
"overrides": [
{
"files": ["test/**/*.js"],
"env": { "mocha": true },
"rules": { "no-unused-expressions": "off" }
}
]
}Its .eslintignore contains dist/ and coverage/.
Install the packages that flat config imports directly:
npm install --save-dev eslint @eslint/js globals eslint-config-prettierThe equivalent eslint.config.mjs:
// eslint.config.mjs
import js from "@eslint/js";
import globals from "globals";
import eslintConfigPrettier from "eslint-config-prettier";
export default [
// Replaces .eslintignore. Only "ignores" here, so it applies globally.
{ ignores: ["dist/", "coverage/"] },
// Replaces extends: ["eslint:recommended"]
js.configs.recommended,
// Replaces top-level env, parserOptions, and rules
{
files: ["**/*.js"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
globals: { ...globals.node },
},
rules: {
"no-unused-vars": "warn",
eqeqeq: ["error", "always"],
},
},
// Replaces the overrides entry
{
files: ["test/**/*.js"],
languageOptions: {
globals: { ...globals.mocha },
},
rules: {
"no-unused-expressions": "off",
},
},
// Replaces extends: ["prettier"]. Keep it last.
eslintConfigPrettier,
];The es2022 env is gone. In eslintrc it enabled modern syntax and modern built-in globals. In flat config, ecmaVersion: "latest" covers the syntax, and ESLint adds the ES built-in globals that match ecmaVersion automatically.
After confirming that the new config works, delete .eslintrc.json and .eslintignore. ESLint v9 ignores .eslintignore and prints a warning if it finds one.
Using the official migrator
For larger configs, start with the migration tool:
npx @eslint/migrate-config .eslintrc.jsonThe tool writes an eslint.config.mjs and prints the packages you need to install. Where it cannot express a shared config natively, it falls back to FlatCompat from @eslint/eslintrc. FlatCompat translates eslintrc-style config into flat objects:
// eslint.config.mjs
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
export default [
...compat.extends("eslint:recommended"),
...compat.env({ node: true }),
];Treat the migrator's output as a first draft. Once every shared config you use publishes a flat version, replace each compat.extends(...) call with a direct import. The native form is shorter and easier to debug.
Gotchas and failure modes
Ignores that silently do nothing globally
An object that has ignores and other keys such as rules or files uses ignores only to narrow that one object. Only an object with ignores as its sole key ignores files globally. If dist/ suddenly shows up in lint output, look for this mistake first. To reuse your .gitignore, use includeIgnoreFile from @eslint/compat.
Files that are no longer linted
Without any files patterns, flat config lints **/*.js, **/*.mjs, and **/*.cjs. The --ext flag no longer exists. If you linted .jsx or .ts files before, add files patterns for them or those files will be skipped.
Changed `sourceType` defaults
Flat config treats .js and .mjs files as ES modules and .cjs files as CommonJS. eslintrc defaulted to scripts. A CommonJS project that uses require in .js files needs sourceType: "commonjs" plus globals.node. Without them you will see parsing errors or no-undef reports for module and require.
Plugins that break at runtime
Some older plugins call context methods that ESLint v9 removed, such as context.getScope(). Those plugins crash with "context.getScope is not a function". Wrap them with fixupPluginRules from @eslint/compat, or wrap whole legacy configs with fixupConfigRules. Upgrading the plugin is the better fix whenever a newer version exists.
Rule names depend on the namespace you choose
In flat config, the key you give a plugin in the plugins object becomes the prefix of its rule names. If you register plugins: { imp: importPlugin }, the rules become imp/no-unresolved, and any existing import/... rules and inline comments stop matching. Keep the namespace the plugin documents.
Verifying the migration
Do not rely on "no errors reported" as proof that the migration worked. A config that skips files will also report no errors. Instead, compare the resolved config for representative files. ESLint v9 can still run the legacy format if you set an environment variable:
ESLINT_USE_FLAT_CONFIG=false npx eslint --print-config test/app.test.js > old.json
npx eslint --print-config test/app.test.js > new.jsonThe two JSON files differ in shape, but compare their rules sections carefully. Run npx eslint --inspect-config to open the config inspector, which shows which array objects match a given file. Finally, run npx eslint . and compare the number of problems with the count from before the migration.
Exercise
After migrating, a teammate's config contains { files: ["**/*.js"], ignores: ["build/"], rules: { semi: "error" } }, and files in build/ are still reported by other rules. Why, and how do you fix it?
Show answer
Because the object has keys besides ignores, its ignores pattern only excludes build/ from that one object (so only the semi setting skips build/). Other objects, such as js.configs.recommended, still lint those files. Move the pattern into its own object containing only ignores, such as { ignores: ["build/"] }, so it applies globally.
Exercise
A legacy CommonJS project had "env": { "node": true } and no parserOptions. After migration, ESLint reports "'require' is not defined" in .js files. What two settings fix it?
Show answer
Flat config treats .js files as ES modules and adds no Node globals by default. Add languageOptions with sourceType: "commonjs" and globals: { ...globals.node } to an object matching */.js.