Course contentsShow
← JavaScript
Lesson 2587 of 2,72352. Linting and FormattingFree lesson

Format on Save Configuration

Configure your editor to automatically format files when saving, ensuring consistent style without manual effort.

Format on Save Configuration

You'll learn how to set up your editor to automatically run Prettier every time you save a file, eliminating manual formatting steps.

The Core Idea

While you can run Prettier manually from the command line or via scripts, the most seamless workflow happens when your editor formats code automatically the moment you hit save. This means you never have to think about formatting—it just happens.

Think of it like autocorrect on your phone. You don't manually fix every typo; your device handles it as you type. Format-on-save does the same for code style.

How It Works

With the Prettier extension installed (covered in the previous lesson), you need to tell your editor: "When I save a JavaScript file, run Prettier on it first."

In VS Code, you configure this in your settings (JSON or UI):

{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode"
}

The first line enables formatting on save globally. The second ensures Prettier is the tool used (not some other formatter).

You can also make this specific to JavaScript files:

{
  "[javascript]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
}

Per-Project Settings

Drop a .vscode/settings.json file in your project root to enforce format-on-save for everyone on the team, ensuring consistent style without relying on individual developer habits.

Key Takeaway: Enabling format-on-save automates code formatting at the moment you save, ensuring consistency without conscious effort and letting you focus on logic instead of commas and semicolons.