Sub topic 14
Migrating from .eslintrc to flat config
This chapter assumes you have read the earlier chapters on flat config, presets, and type-aware linting. You should be comfortable with ES modules, with tsconfig.json, and with running ESLint through npm scripts. The goal is to convert an existing .eslintrc setup into an eslint.config.mjs file and to avoid the mistakes that usually come up on the way.
Why the old format is going away
ESLint used to read configuration from .eslintrc.json, .eslintrc.js, or a package.json key. It merged files found in parent directories, resolved plugins by name, and resolved extends strings relative to the config file. That behavior was hard to predict and hard to extend.
Flat config replaces it with one array of plain objects in a file named eslint.config.js, .mjs, or .cjs. Each object can say which files it applies to. Later objects override earlier ones. Plugins and parsers are imported as normal JavaScript values instead of being looked up by string.
The format is the default in ESLint 9. Version 9 ignores .eslintrc.* files unless you set ESLINT_USE_FLAT_CONFIG=false. ESLint 8.57 can run flat config if you set ESLINT_USE_FLAT_CONFIG=true. This lets you test the new file before you upgrade.
Mapping old concepts to new ones
Most of the work is renaming. The table shows where each legacy key goes.
| Legacy `.eslintrc` | Flat config |
|---|---|
| `root: true` | Not needed. There is no cascade of config files to stop. |
| `extends: ["eslint:recommended"]` | `eslint.configs.recommended` from `@eslint/js` |
| `extends: ["plugin:@typescript-eslint/recommended"]` | `tseslint.configs.recommended` |
| `plugins: ["@typescript-eslint"]` | Included by the typescript-eslint presets. Otherwise `plugins: { name: importedPlugin }`. |
| `parser: "@typescript-eslint/parser"` | `languageOptions.parser`. The presets set it for you. |
| `parserOptions` | `languageOptions.parserOptions`, plus `ecmaVersion` and `sourceType` directly under `languageOptions` |
| `env: { node: true }` | `languageOptions.globals`, using the `globals` package |
| `overrides: [{ files, rules }]` | A separate array entry with `files` |
| `ignorePatterns` and `.eslintignore` | An entry that contains only `ignores` |
| `settings` | `settings`, unchanged |
| `reportUnusedDisableDirectives` | `linterOptions.reportUnusedDisableDirectives` |
Two of these rows matter most for TypeScript projects. The first is plugins: a legacy config names them as strings, but a flat config holds the plugin object itself, under a key of your choosing. The second is the parser. In a legacy config you set parser and plugins by hand. With the typescript-eslint presets you usually set neither.
A worked conversion
Here is a typical legacy setup.
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": true,
"tsconfigRootDir": "."
},
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"env": { "node": true, "es2022": true },
"ignorePatterns": ["dist/", "coverage/"],
"rules": {
"@typescript-eslint/no-explicit-any": "error"
},
"overrides": [
{
"files": ["*.js", "*.cjs"],
"rules": { "@typescript-eslint/no-var-requires": "off" }
},
{
"files": ["**/*.test.ts"],
"rules": { "@typescript-eslint/unbound-method": "off" }
}
]
}The flat version follows. It needs two installs first: npm install --save-dev globals and the packages from earlier chapters (eslint, @eslint/js, typescript-eslint).
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import globals from 'globals';
import { defineConfig, globalIgnores } from 'eslint/config';
export default defineConfig(
globalIgnores(['dist/', 'coverage/']),
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
globals: { ...globals.node },
ecmaVersion: 2022,
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
rules: {
'@typescript-eslint/no-explicit-any': 'error',
},
},
{
files: ['**/*.js', '**/*.cjs'],
extends: [tseslint.configs.disableTypeChecked],
rules: {
'@typescript-eslint/no-require-imports': 'off',
},
},
{
files: ['**/*.test.ts'],
rules: {
'@typescript-eslint/unbound-method': 'off',
},
},
);Several changes in this file are worth reading closely:
recommended-requiring-type-checkingwas renamed. In typescript-eslint v6 and later, the equivalent preset isrecommendedTypeChecked.parserOptions.project: truebecameprojectService: true. The project service is the current way to give rules type information.- The old
no-var-requiresrule was replaced byno-require-importsin newer typescript-eslint versions, so the override uses the new name. Check the rule list of your installed version before you carry rule names across. - The JavaScript override now calls
disableTypeChecked. Legacy configs often did not need this, because they did not apply type-checked rules to files outside the TypeScript project. - The
ignorePatternslist becameglobalIgnores, which produces an entry that has onlyignores.
Let a tool do the first pass
ESLint ships a migration helper that reads a legacy config and writes a flat one.
npx @eslint/migrate-config .eslintrc.jsonIt creates eslint.config.mjs. Treat the output as a draft. It tends to wrap legacy extends strings in a compatibility layer, which keeps working but hides what is actually applied. After running it, replace the compatibility calls with direct imports wherever the plugin or preset now ships a flat config. Then delete the old .eslintrc.* and .eslintignore files so nobody wonders which one is in effect.
Plugins that have no flat config yet
Some older plugins and shared configs only export legacy format. The FlatCompat class from @eslint/eslintrc converts them at runtime.
// eslint.config.mjs
import { FlatCompat } from '@eslint/eslintrc';
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
const compat = new FlatCompat({
baseDirectory: import.meta.dirname,
});
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
...compat.extends('some-legacy-shared-config'),
);Install @eslint/eslintrc as a dev dependency first. Keep this as a stopgap. Compatibility output can differ from a native flat config in subtle ways, such as how plugin names are resolved, and each shim is a dependency you will eventually remove. Check the plugin's documentation or release notes for a flat export before you rely on FlatCompat.
Errors you are likely to hit
No config found. The message "ESLint couldn't find an eslint.config.(js|mjs|cjs) file" means ESLint 9 does not see a flat file. Check the file name and the directory you ran the command from.
TypeScript files are not linted. By default, flat config matches only .js, .mjs, and .cjs files. The typescript-eslint presets add patterns for .ts, .tsx, .mts, and .cts. If you wrote your own config object with a parser and no preset, add files: ['**/*.ts'] to it.
An ignore does nothing. An ignores key in an object that also has rules or other keys only limits that one object. To exclude files from linting entirely, use an object that contains nothing but ignores, or use globalIgnores. Also note that node_modules and .git are ignored by default, so you can drop those entries.
Type-checked rules crash on JavaScript files. Rules that need types fail with a message that the file is not part of a TypeScript project. Apply tseslint.configs.disableTypeChecked to those files, as in the example above.
Different results after migrating. Legacy config merged files from parent directories, and flat config does not. If a nested .eslintrc was adding rules for one package, move those rules into an entry with a files pattern for that package.
Module format mismatch. An eslint.config.js file follows your package.json type field. If you see syntax errors on import, rename the file to eslint.config.mjs.
Updating scripts, editors, and CI
Remove --ext flags and --ignore-path from package.json scripts. File selection now comes from files and ignores in the config. A script like eslint . --ext .ts,.tsx becomes eslint ..
Editors need a recent ESLint extension. The VS Code extension detects eslint.config.* automatically in current versions. If you pin an old extension version, upgrade it before blaming the config. In CI, the commands are the same as before, but make sure the Node version meets ESLint 9's minimum, which is Node 18.18 or newer in the 9.x series.
npx eslint .
npx eslint . --fixExercise
A legacy config has "ignorePatterns": ["build/"] and an override { "files": ["scripts/*.js"], "env": { "node": true } }. Write the two flat config entries that replace them, and say which languageOptions key carries the environment.
Show answer
Use globalIgnores(['build/']) (or an object containing only ignores: ['build/']) and { files: ['scripts/*.js'], languageOptions: { globals: { ...globals.node } } }, with import globals from 'globals'. The environment moves to languageOptions.globals.
Exercise
After migration, ESLint reports no problems for any .ts file, although the old config reported many. You wrote a config object with languageOptions.parser set to the typescript-eslint parser and nothing else. What is the likely cause, and what is the fix?
Show answer
Flat config only matches .js, .mjs, and .cjs files unless an object's files pattern covers other extensions. Add files: ['**/*.ts'] to the object, or use a typescript-eslint preset, which already includes the TypeScript file patterns.