Sub topic 12
Editor Integration and Format on Save
By this point in the tutorial, ESLint and Prettier are configured, and eslint-config-prettier keeps their rules from overlapping. You can run both from the terminal. This chapter moves that work into the editor, so problems show up as you type and files get fixed and formatted when you save.
This chapter assumes you already have:
- An
eslint.config.jsflat config that ends witheslintConfigPrettier. - A
.prettierrcfile and Prettier installed as a dev dependency. - Visual Studio Code (VS Code). The ideas carry over to other editors, and a short section near the end covers them.
The working model
Editor plugins do not ship their own rules. They are thin clients that find the ESLint and Prettier copies in your project's node_modules, load your config files, and run on the open file. If the editor and the terminal disagree, the cause is almost always that they are running different versions or reading different configs.
On save, VS Code goes through two separate steps:
- Code actions on save run first. This is where ESLint's autofix (the same fixes as
eslint --fix) is applied. - Format on save runs second. The configured default formatter rewrites the file. For JavaScript, that formatter should be Prettier.
The order matters very little here. Because eslint-config-prettier turns off ESLint's formatting rules, an ESLint fix and a Prettier rewrite never compete for the same characters. Without that config, an ESLint fix could undo Prettier's output, and saving twice could give you two different files.
Installing the extensions
Install the two official extensions:
| Extension | Marketplace ID | What it does |
|---|---|---|
| ESLint | `dbaeumer.vscode-eslint` | Shows lint problems inline and provides the `source.fixAll.eslint` code action |
| Prettier - Code formatter | `esbenp.prettier-vscode` | Registers Prettier as a formatter VS Code can use |
To make your teammates get the same prompt, commit a .vscode/extensions.json file. VS Code offers to install the listed extensions when someone opens the folder.
{
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
}Workspace settings
Put the settings in .vscode/settings.json and commit that file. User settings only affect your own machine. Workspace settings travel with the repository, so every contributor saves files the same way.
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"prettier.requireConfig": true
}Here is what each setting does:
editor.formatOnSaveturns on formatting at save time.editor.defaultFormatterpicks Prettier when more than one formatter could handle a file. VS Code ships a built-in JavaScript formatter, so without this setting you may get the wrong one, or a prompt asking you to choose.- The
[javascript]and[json]blocks are language-specific overrides. They take priority over the global value. This matters if a user-level setting assigns a different formatter to one language. "source.fixAll.eslint": "explicit"applies ESLint fixes when you save explicitly (Ctrl+S or Cmd+S). It does not run on auto-save after a delay or on focus change. The older valuetruestill works and is treated as"explicit"."always"also fixes on auto-save.prettier.requireConfigtells the extension to format only when it finds a Prettier config file. This stops it from reformatting files in unrelated projects that never chose Prettier.
Which Prettier and ESLint the editor uses
The ESLint extension resolves the eslint package from the workspace. If your project has no local ESLint, the extension reports an error rather than silently using some other version. That behavior is what you want.
The Prettier extension prefers the project's local prettier. If it cannot find one, it falls back to a copy bundled inside the extension. That fallback hides version drift: the editor formats with one Prettier version and npm run format:check in CI uses another, so CI fails on files that looked fine locally. Keep prettier in devDependencies and run npm install after cloning.
To check which copy is running, open the Output panel (View > Output) and choose Prettier or ESLint from the dropdown. Both extensions log the module path and config file they loaded.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Saving does nothing | `files.autoSave` is set to `afterDelay` | Format on save does not run on delayed auto-save. Save explicitly, or switch auto-save to `onFocusChange` |
| A "multiple formatters" prompt appears | No default formatter for the language | Set `editor.defaultFormatter`, globally or per language |
| No ESLint squiggles anywhere | ESLint is not installed locally, or the config failed to load | Check the ESLint Output panel. Run `npx eslint yourfile.js` to see the real error |
| ESLint works in the terminal but not in a monorepo package | The extension resolves the config from the wrong folder | Set `eslint.workingDirectories`, for example `[{ "mode": "auto" }]` |
| Formatting flips back and forth on each save | An ESLint formatting rule fights Prettier | Make sure `eslintConfigPrettier` is the last entry in the config |
| Squiggles show old results after a config edit | The ESLint server cached the previous config | Run **ESLint: Restart ESLint Server** from the Command Palette |
If you want to format only the lines you changed, set "editor.formatOnSaveMode": "modifications". This only works in a folder under source control, and it is a stopgap: your checks still see unformatted code in lines nobody has touched.
Other editors
The model is the same in every editor: a client runs your project's local tools against your project's configs.
- WebStorm and other JetBrains IDEs include ESLint and Prettier support. Under Settings > Languages & Frameworks > JavaScript, choose Automatic ESLint configuration and enable Prettier's Run on save.
- Neovim usually uses the
eslintlanguage server (from thevscode-langservers-extractedpackage) for diagnostics, plus a formatter plugin such asconform.nvimthat callsprettierfromnode_modules/.bin.
Whichever editor you use, the editor setup is a convenience. It does not enforce anything. A contributor without the extensions can still commit unformatted or failing code. The next chapters cover the npm scripts, pre-commit hooks, and CI checks that do enforce the rules.
Exercise
A teammate reports that saving a file in VS Code applies Prettier, but unused imports and == comparisons are never auto-fixed, even though npx eslint --fix fixes them. Their settings include "editor.formatOnSave": true and "editor.defaultFormatter": "esbenp.prettier-vscode". What is missing, and why doesn't format on save cover it?
Show answer
They are missing "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit" }. Format on save only runs the default formatter, which is Prettier. ESLint fixes are delivered as a code action, so they run only when code actions on save are configured. (Also note that unused-variable reports from no-unused-vars have no autofix. The fixable part here is eqeqeq, and only in cases where ESLint can prove the change is safe.)
Exercise
CI fails prettier . --check on a file that the developer says was formatted on save. The ESLint and Prettier Output panels show the Prettier extension loaded a module from inside the extension folder. What happened, and how do you prevent it?
Show answer
The project's local prettier was not installed (for example, npm install was never run), so the extension fell back to its bundled Prettier, which is a different version from the one CI uses. Keep prettier pinned in devDependencies, run npm install, and confirm in the Output panel that the extension resolves Prettier from the project's node_modules.
Exercise
Why should .vscode/settings.json and .vscode/extensions.json be committed to the repository instead of each developer configuring user settings?
Show answer
Workspace files travel with the repository, so every contributor gets the same formatter, the same save actions, and a prompt to install the same extensions. User settings differ from machine to machine, which leads to inconsistent saves and noisy diffs.