Sub topic 11
Making ESLint and Prettier Work Together
This chapter assumes you already have ESLint running with an eslint.config.js flat config and Prettier installed with a .prettierrc file. Here you will stop the two tools from contradicting each other.
Where the conflict comes from
ESLint can enforce style as well as correctness. Some core rules and many plugin rules care about indentation, quotes, semicolons, or line length. Prettier also decides those things, and it ignores whatever ESLint thinks. If the two disagree, you get a loop: Prettier rewrites a line, ESLint flags the result, and eslint --fix changes it back.
The fix is to divide the work cleanly:
| Concern | Owner | Examples |
|---|---|---|
| Code quality | ESLint | `no-unused-vars`, `eqeqeq`, `no-undef` |
| Formatting | Prettier | quotes, semicolons, wrapping, indentation |
Installing eslint-config-prettier
eslint-config-prettier contains no rules of its own. It is a config object that sets every formatting-related rule it knows about to "off".
npm install --save-dev eslint-config-prettierAdd it as the last entry in your config array:
// eslint.config.js
import js from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier";
export default [
js.configs.recommended,
// Keep this last so it turns off rules that conflict with Prettier
eslintConfigPrettier,
];Order matters because flat config merges entries from top to bottom, and later entries override earlier ones. If you put a shared config after eslintConfigPrettier, that shared config can switch formatting rules back on.
Checking for leftover conflicts
The package includes a small CLI. It reads the config ESLint resolves for a given file and lists any enabled rules that conflict with Prettier:
npx eslint-config-prettier src/index.jsIf it reports nothing, the two tools agree for that file.
What about eslint-plugin-prettier?
eslint-plugin-prettier takes a different approach. It runs Prettier inside ESLint and reports formatting differences as lint errors. This gives you a single command, but it has costs:
- Linting gets slower because Prettier runs on every file.
- Your editor fills with red squiggles for spacing, which hides the real problems.
- You still need eslint-config-prettier to turn off the conflicting rules.
Prettier's own documentation recommends running the two tools separately. Use eslint-config-prettier on its own, then run eslint and prettier as two separate steps. Later chapters wire those steps into your editor, scripts, hooks, and CI.
Common mistakes
- Putting
eslintConfigPrettierfirst in the array, so every later config can override it. - Expecting eslint-config-prettier to format code. It only turns rules off. Prettier does the formatting.
- Keeping stylistic rules such as
quotesorsemiin your own rules block "just in case."
Exercise
Your config array is [eslintConfigPrettier, js.configs.recommended, somePlugin.configs.recommended], and somePlugin enables "indent": "error". Prettier and ESLint now fight over indentation. What is the minimal fix?
Show answer
Move eslintConfigPrettier to the end of the array. Flat config applies entries in order, so the plugin's later "indent": "error" overrides the earlier "off". As the last entry, eslint-config-prettier switches "indent" off again.
Exercise
Why is "npx eslint-config-prettier src/index.js" useful after you add a new plugin?
Show answer
New plugins can enable formatting rules. The CLI checks the config ESLint resolves for that file and lists any enabled rules that conflict with Prettier, so you can confirm nothing overrides eslint-config-prettier.