Sub topic 14
Migrating from `.eslintrc` to Flat Config
This chapter assumes that you can read TypeScript, use npm and Node.js, and understand ES module imports and exports. It also assumes that your project already has a tsconfig.json and an existing ESLint configuration.
ESLint’s flat config replaces .eslintrc, .eslintrc.json, and .eslintrc.js with an eslint.config.js or eslint.config.mjs file. The new format uses one exported array of configuration objects. ESLint evaluates those objects from top to bottom.
Map old concepts to new concepts
The migration changes configuration structure, not the purpose of your lint rules.
extendsbecomes imported configuration objects placed in the exported array.pluginschanges from an array of package names to an object mapping names to imported plugins.parsermoves underlanguageOptions.parser.parserOptionsmoves underlanguageOptions.parserOptions.envis replaced by imported globals or explicitlanguageOptions.globals.overridesbecomes a configuration object with afilesproperty.ignorePatternsbecomes an object with anignoresproperty.- Rule names still use the plugin prefix, such as
@typescript-eslint/no-explicit-any.
The old configuration might look like this:
// .eslintrc.cjs
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
env: {
node: true,
es2022: true,
},
rules: {
'@typescript-eslint/no-explicit-any': 'error',
},
};The equivalent native flat config is:
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import globals from 'globals';
import { defineConfig } from 'eslint/config';
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
{
files: ['**/*.ts', '**/*.tsx'],
languageOptions: {
globals: {
...globals.node,
},
},
rules: {
'@typescript-eslint/no-explicit-any': 'error',
},
},
);The imported tseslint package supplies the TypeScript parser, plugin, and preset configuration. You no longer name the parser as a string. The configuration object applies only to TypeScript files because of its files pattern.
Migrate in small steps
- Upgrade ESLint and the related packages.
- Create
eslint.config.mjs. - Import the JavaScript and TypeScript presets that replace your old
extendsentries. - Move parser settings into
languageOptions. - Convert plugins from strings to imported objects.
- Convert
overridesentries into objects withfiles. - Move ignore patterns into an
ignoresobject. - Run ESLint and compare its reports with the old configuration.
- Remove the old
.eslintrcfile after the new configuration behaves as expected.
Flat config searches for eslint.config.js, eslint.config.mjs, or eslint.config.cjs from the working directory. Keep the file at the project root unless you have a deliberate reason to use another location.
Use a compatibility adapter when necessary
FlatCompat from @eslint/eslintrc can translate many legacy configurations while you migrate:
// eslint.config.mjs
import { FlatCompat } from '@eslint/eslintrc';
import { defineConfig } from 'eslint/config';
const compat = new FlatCompat({
baseDirectory: import.meta.dirname,
});
export default defineConfig(
...compat.extends(
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
),
);This approach helps with older shareable configs, but it adds an adapter layer. Replace it with native flat config when the packages you depend on support that format.
Exercise
In flat config, where do a legacy parser and parserOptions belong?
Show answer
Put the parser in languageOptions.parser and the parser options in languageOptions.parserOptions.