Sub topic 5
Building on Shared Configs and Plugins
This chapter assumes you have ESLint installed, that you can read an eslint.config.js file, and that you know the three severity levels ("off", "warn", "error"). It explains how to start from rule sets other people maintain, how to add rules that ESLint does not ship with, and how to change those rules without forking them.
Why start from a shared config
ESLint has a few hundred core rules. Choosing each one by hand takes time and tends to give different results in each project. A shared config is a ready-made config object, or array of config objects, that you put into your own config array. A plugin is an object that supplies extra rules, and often extra shared configs, under a namespace.
Most projects end up with this structure:
- A base shared config such as
js.configs.recommended. - Plugin configs for the environment or framework, such as Node.js, React, or TypeScript.
- Your own overrides, placed last.
How the config array merges
A flat config is an array, and ESLint reads it in order. For each file it lints, ESLint keeps only the objects whose files pattern matches that file (an object without files matches everything). It then merges those objects from first to last. When two objects set the same key, the later one wins.
Array order decides everything. If you put your overrides before the shared config, the shared config overwrites them without any warning.
Using ESLint's built-in shared configs
The @eslint/js package exports two configs:
| Config | What it enables | Typical use |
|---|---|---|
| `js.configs.recommended` | Rules that catch likely bugs, set to `"error"` | The default starting point for most projects |
| `js.configs.all` | Every core rule | Auditing what is available. It is too noisy for daily use, and it changes with every ESLint release |
Overriding a rule from a shared config only needs a later object:
// eslint.config.js
import js from "@eslint/js";
export default [
js.configs.recommended,
{
rules: {
// recommended sets this to "error"; relax it
"no-unused-vars": "warn",
// recommended leaves this off; turn it on
"prefer-const": "error",
},
},
];Adding a plugin
A plugin's rules are invisible until you register the plugin under a namespace in a plugins object. Its rules are then named namespace/rule-name. The example below uses eslint-plugin-n, which adds Node.js-specific checks.
npm install --save-dev eslint @eslint/js eslint-plugin-n globals// eslint.config.js
import js from "@eslint/js";
import nodePlugin from "eslint-plugin-n";
export default [
js.configs.recommended,
{
files: ["**/*.js"],
plugins: { n: nodePlugin },
rules: {
"n/no-process-exit": "error",
"n/prefer-node-protocol": "warn",
},
},
];The key n is the namespace you chose. You could register the same plugin as node, and the rules would become node/no-process-exit. Plugin documentation shows a conventional namespace, and its own shared configs depend on that name, so keep the documented one.
Using a plugin's shared configs
Most plugins also export shared configs. These configs register the plugin for you and turn on a recommended set of its rules:
// eslint.config.js
import js from "@eslint/js";
import nodePlugin from "eslint-plugin-n";
export default [
js.configs.recommended,
nodePlugin.configs["flat/recommended"],
{
rules: {
"n/no-process-exit": "off",
},
},
];Plugins name their configs in different ways. You may see configs.recommended, configs["flat/recommended"], or a config that is itself an array. Check the plugin's README. If a plugin only documents .eslintrc-style extends strings, it may not support flat config yet. The migration chapter covers the compatibility tools for that case.
Scoping a shared config to some files
Shared configs usually have no files key, so they apply to every file. To limit one to part of the project, spread it into a new object and add files:
// eslint.config.js
import js from "@eslint/js";
import nodePlugin from "eslint-plugin-n";
import globals from "globals";
export default [
js.configs.recommended,
{
...nodePlugin.configs["flat/recommended"],
files: ["server/**/*.js"],
},
{
files: ["client/**/*.js"],
languageOptions: { globals: { ...globals.browser } },
},
];If the shared config is an array, map over it: ...someConfigArray.map((c) => ({ ...c, files: ["server/**/*.js"] })).
ESLint 9.22 and later also provide defineConfig() with an extends key, which does this scoping for you:
// eslint.config.js
import { defineConfig } from "eslint/config";
import js from "@eslint/js";
export default defineConfig([
{
files: ["**/*.js"],
plugins: { js },
extends: ["js/recommended"],
},
]);Common errors and gotchas
| Symptom | Cause | Fix |
|---|---|---|
| `Could not find "rule" in plugin "x"` | Wrong rule name, or the rule was renamed in a new plugin version | Check the plugin's rule list for your installed version |
| `A configuration object specifies rule "x/..." but could not find plugin "x"` | The rule is used in an object that does not match the same files as the object that registers the plugin | Register the plugin in an object that covers those files, or remove `files` from it |
| `Cannot redefine plugin "x"` | Two different plugin objects registered under one namespace, often from two installed copies of the plugin | Remove the duplicate copy (`npm ls eslint-plugin-x`) or reuse the same import |
| Your override has no effect | It comes before the shared config in the array | Move overrides to the end |
| New errors after `npm update` | Shared configs add rules in major versions | Pin major versions and read changelogs before upgrading |
To find out which config produced a rule setting, run one of these:
npx eslint --print-config server/index.js
npx eslint --inspect-config--print-config prints the final merged config for one file. --inspect-config opens a browser tool that shows which objects matched the file and what each one contributed.
Publishing your own shared config
If several repositories use the same rules, publish them as a package. By convention the package is named eslint-config-<name> or @scope/eslint-config, and its main module exports an array:
// index.js of @acme/eslint-config
import js from "@eslint/js";
import nodePlugin from "eslint-plugin-n";
export default [
js.configs.recommended,
nodePlugin.configs["flat/recommended"],
{
rules: {
eqeqeq: ["error", "always"],
"no-unused-vars": "warn",
},
},
];Each project then uses the package and adds its own changes after it:
// eslint.config.js in a consuming project
import acme from "@acme/eslint-config";
export default [
...acme,
{ rules: { "no-unused-vars": "error" } },
];List eslint as a peerDependency of the package, so that projects supply their own copy. Plugins can be regular dependencies, because the config imports them itself. If you set "type": "module" in the package's package.json, consumers can import it without a build step.
Exercise
Your config is [{ rules: { eqeqeq: "warn" } }, js.configs.recommended], and eqeqeq is still reported as an error. Why, and what is the fix?
Show answer
Objects later in the array override earlier ones. Here the recommended config comes after your override, so its setting replaces yours. Move your override object after js.configs.recommended.
Exercise
You register eslint-plugin-n as plugins: { node: nodePlugin } and then add nodePlugin.configs["flat/recommended"] to the same array. What goes wrong?
Show answer
The plugin's shared config registers the plugin under the namespace "n" and turns on rules named "n/...". Your rules use "node/...", so the plugin is now loaded under two namespaces, and your overrides target different rule names than the ones the shared config enabled. Use the documented "n" namespace everywhere.
Exercise
How do you apply a shared config object sharedCfg only to files under scripts/, using a plain exported array without defineConfig?
Show answer
Spread it into a new object and add a files key: { ...sharedCfg, files: ["scripts/**/*.js"] }. If sharedCfg is an array, map over it and add files to each element.