Switching to ESLint's flat config format

Version 8 of ESLint introduced a new configuration format called Flat Config. The next major version will use this config format by default. The purpose of this format is to:

  • push towards a single configuration format (in contrast to the existing JSON, Yaml and JS-based configs)
  • enforce explicit native loading (instead of the implicit imports in JSON and Yaml)
  • use a flat cascading of rules (instead of a mix of rules and overrides)

See below a direct comparison between JSON, JS and Flat config:

eslint.config.js
1// the older versions were magically interpreting all the imports 2// in flat config we do it explicitly 3const nxPlugin = require('@nx/eslint-plugin'); 4const js = require('@eslint/js'); 5const baseConfig = require('./eslint.base.config.js'); 6const globals = require('globals'); 7const jsoncParser = require('jsonc-eslint-parser'); 8const tsParser = require('@typescript-eslint/parser'); 9 10module.exports = [ 11 js.configs.recommended, 12 // this will spread the export blocks from the base config 13 ...baseConfig, 14 { plugins: { '@nx': nxPlugin } }, 15 { 16 languageOptions: { 17 parser: tsParser, 18 globals: { 19 ...globals.node, 20 }, 21 }, 22 rules: { 23 '@typescript-eslint/explicit-module-boundary-types': ['error'], 24 }, 25 }, 26 // there are no overrides, all the config blocks are "flat" 27 { 28 files: ['*.json'], 29 languageOptions: { 30 parser: jsoncParser, 31 }, 32 rules: {}, 33 }, 34 { 35 files: ['*.ts', '*.tsx', '*.js', '*.jsx'], 36 rules: { 37 '@nx/enforce-module-boundaries': [ 38 'error', 39 { 40 enforceBuildableLibDependency: true, 41 allow: [], 42 depConstraints: [ 43 { 44 sourceTag: '*', 45 onlyDependOnLibsWithTags: ['*'], 46 }, 47 ], 48 }, 49 ], 50 }, 51 }, 52]; 53
Nx 15 and lower use @nrwl/ instead of @nx/

For additional details, head over to ESLint's official blog post.

Since version 16.8.0, Nx supports the usage of flat config in the @nx/eslint:lint executor and @nx/* generators, and provides an automated config conversion from .eslintrc.json config files.

Converting workspace from .eslintrc.json to flat config

To convert workspace ESLint configurations from the default .eslintrc.json to the new flat config you need to run:

nx g @nx/eslint:convert-to-flat-config

Nx 15 and lower use @nrwl/ instead of @nx/

The generator will go through all the projects and convert their configurations to the new format. It will also convert the base .eslintrc.json and .eslintignore.

Correctness and best practices

The purpose of this generator is to create a flat config that works the same way as the original JSON config did. Depending on the complexity of your original config, it may be using the FlatCompat utility to provide a compatibility wrapper around parts of the original config. You can improve those by following the official migration guide.