Sub topic 5
Building on Shared Configs and Plugins
This chapter assumes you can read an eslint.config.js file and set rule severities to "off", "warn", or "error". It shows how to start from rule sets other people maintain and adjust them for your project, so you don't have to list every rule yourself.
Shared configs are just config objects
A shared config is a config object, or an array of config objects, that someone publishes for reuse. js.configs.recommended from @eslint/js is one example. It is a plain object with a rules property that turns on rules most projects want.
Your exported config is an array, and ESLint merges every entry that matches a file in order. When two entries set the same rule, the later entry wins. That gives you one working model: put shared configs first and your own overrides last.
Plugins add new rules
A shared config can only switch on rules that already exist. A plugin is a package that adds new rules. In flat config, you import the plugin and register it under a namespace in the plugins key. Its rules then take the form namespace/rule-name.
Install the Node.js plugin as an example:
npm install --save-dev eslint @eslint/js globals eslint-plugin-nMost plugins also ship their own recommended configs. A recommended config usually registers the plugin for you, so you can drop it straight into the array:
// eslint.config.js
import js from "@eslint/js";
import globals from "globals";
import nodePlugin from "eslint-plugin-n";
export default [
js.configs.recommended,
nodePlugin.configs["flat/recommended"],
{
files: ["**/*.js"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
globals: { ...globals.node },
},
rules: {
"no-unused-vars": "warn",
"n/no-process-exit": "off",
},
},
];If you only want a few rules from a plugin, register the plugin yourself and skip its preset:
// eslint.config.js
import nodePlugin from "eslint-plugin-n";
export default [
{
plugins: { n: nodePlugin },
rules: {
"n/no-missing-import": "error",
},
},
];Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `Could not find plugin "n"` | A rule uses a namespace that no entry registers | Add the plugin under `plugins`, or include its preset |
| Your override has no effect | The shared config comes after your override | Move your override entry to the end of the array |
| `Unexpected key "extends"` or a plugin given as a string | An `.eslintrc`-style example copied into flat config | Import the plugin object and spread or list its flat config |
| A preset name isn't found | The plugin names its flat presets differently | Check the plugin's README for the exact key, such as `"flat/recommended"` |
Exercise
Your config array is [myOverrides, js.configs.recommended], and myOverrides sets "no-unused-vars": "off". ESLint still reports unused variables as errors. Why, and how do you fix it?
Show answer
ESLint merges entries in order, and later entries win. js.configs.recommended comes after your override, so it sets no-unused-vars back to "error". Put js.configs.recommended first and myOverrides last.
Exercise
You add "import/no-unresolved": "error" to your rules and ESLint reports that it cannot find the plugin "import". What is missing?
Show answer
No config entry registers a plugin under the import namespace. Import the plugin package and add it as plugins: { import: importPlugin }, or include a preset from the plugin that registers it.