Code Formatting

Prettier

Prettier is the opinionated code formatter that ends style debates. It parses your code and reprints it consistently, so nobody argues about spacing, quotes or semicolons.

beginner12 min readUpdated Sep 15, 2026
.prettierrc
json
// .prettierrc
{
  "semi": true,
  "singleQuote": false,
  "trailingComma": "all",
  "printWidth": 100,
  "arrowParens": "always"
}
What it is
An opinionated formatter
Approach
Parse and reprint
Config
A few options
Languages
JS, TS, CSS, HTML, JSON, MD
CI
--check fails on diff
Editors
Format on save

Why it matters

Why Prettier ends arguments

One consistent style

Every file looks the same regardless of who wrote it, which makes diffs smaller and reviews faster.

No style debates

The formatter decides. Teams stop spending review time on spacing, quotes and line breaks.

Runs everywhere

Format from the CLI, on save in your editor, in a pre-commit hook and as a check in CI.

The big picture

The three ideas behind Prettier

A parser, a deterministic printer and a small set of options. Formatting is decided once and applied everywhere.

The parser

Read

Understands the syntax of each supported language and builds a syntax tree.

The printer

Format

Reprints the tree with a deterministic layout, ignoring your original whitespace.

The config

Options

A handful of options and ignore files control the few decisions left to you.

Prettier at a glance

The core of Prettier

.prettierrc

A small config file for the options you actually want to change.

prettier --write

Format files in place from the command line.

prettier --check

Fail if any file is not formatted, ideal for CI.

Editor integration

Format on save so files are always formatted as you work.

eslint-config-prettier

Disables ESLint rules that would conflict with Prettier.

.prettierignore

Skip build output, generated files and dependencies.

A short history

The formatter that made style a solved problem

  1. 2017

    Prettier released

    James Long introduces an opinionated formatter that reprints code from its AST.

    17
  2. 2018

    Mainstream adoption

    Prettier becomes standard in JavaScript, TypeScript, CSS and markdown projects.

    18
  3. 2020

    Prettier 2

    New defaults and better TypeScript and JSX support.

    20
  4. 2023

    Prettier 3

    ESM-first, async plugins and improved performance.

    23
  5. Today

    The formatting default

    Used by most teams and built into many framework scaffolds.

    Today

The complete guide

Prettier: Everything you need to know

What is Prettier?

Prettier is an opinionated code formatter. It parses your code into a syntax tree and reprints it with a deterministic layout, ignoring the whitespace you wrote. The output depends only on the code’s structure and a small set of options, so the same input always produces the same output.

That determinism is the point. Style arguments disappear because the formatter decides, not the team. Diffs shrink to actual changes instead of spacing churn, and reviewers spend their time on logic and design. Prettier is one of the few tools that measurably reduces friction in a codebase.

Configuration

Prettier works with no configuration, but you can set a few options in .prettierrc.

{
  "semi": true,
  "singleQuote": false,
  "trailingComma": "all",
  "printWidth": 100,
  "tabWidth": 2,
  "arrowParens": "always"
}

These are the options most teams touch. Beyond them, Prettier makes the decisions. The fewer options you set, the more consistent the result across projects and the less there is to maintain.

Running Prettier

Format files in place, or check without writing.

npx prettier --write .
npx prettier --check .
npx prettier --write src/app.tsx

--write reformats and saves; --check reports files that would change and exits non-zero, which is exactly what CI needs. Add a script so the commands are consistent.

{
  "scripts": {
    "format": "prettier --write .",
    "format:check": "prettier --check ."
  }
}

Ignoring files

.prettierignore lists paths to skip, using the same syntax as .gitignore.

dist
coverage
pnpm-lock.yaml
*.generated.ts

Ignoring build output and generated files matters: formatting them produces noise that the next build overwrites, and it can even break code generation that expects a specific layout.

Editor integration

Install the Prettier extension for your editor and enable format on save. This is where Prettier pays off most: you never think about formatting, and every save leaves the file clean. It also means you can use --check in CI with confidence, because formatting happens continuously rather than in a big cleanup at the end.

Working with ESLint

Prettier and ESLint overlap on style rules, and left alone they will fight. The standard solution is to let Prettier own formatting and disable the conflicting ESLint rules.

// eslint.config.js
import js from "@eslint/js";
import prettier from "eslint-config-prettier";

export default [js.configs.recommended, prettier];

eslint-config-prettier turns off every ESLint rule that would conflict with Prettier, so ESLint focuses on code quality and Prettier on layout. If you want formatting problems surfaced as lint errors, eslint-plugin-prettier runs Prettier as a rule, though many teams prefer to keep the two separate.

Pre-commit hooks

Formatting before a commit keeps CI green and history clean.

{
  "lint-staged": {
    "*.{js,jsx,ts,tsx,css,md}": "prettier --write"
  }
}

Combined with a hook runner such as husky, this formats only the staged files, so commits stay fast. It is a small amount of setup that prevents a whole class of “unformatted code” review comments.

Where Prettier fits

Prettier is formatting only. It does not find bugs, enforce types or check imports — those belong to ESLint and the type checker. A healthy toolchain runs all three: Prettier for layout, ESLint for quality and TypeScript for types. Each has one job, and none of them fights the others.

Best practices

  • Accept the defaults unless your team has a strong reason to change one.
  • Commit a .prettierrc so every editor and CI run agrees.
  • Add .prettierignore for build output and generated files.
  • Format on save in the editor.
  • Run --check in CI and --write in a pre-commit hook.
  • Disable conflicting ESLint rules with eslint-config-prettier.
  • Keep the option list short.

Common mistakes

  • Letting ESLint and Prettier both format and conflict.
  • Formatting generated files and creating noisy diffs.
  • Skipping the CI check, so unformatted code merges.
  • Changing options per project for no reason and losing cross-project consistency.
  • Running Prettier on the whole repo in a pre-commit hook and slowing commits.
  • Using Prettier to enforce semantic rules that belong to ESLint.

Where to go next

Prettier removes a whole category of busywork. Pair it with ESLint for quality, TypeScript for type safety, and wire both into your Vite project and CI. Then set up format on save and never think about spacing again.

Dividing responsibilities

Let Prettier format and ESLint catch bugs. Disabling ESLint's stylistic rules avoids two tools fighting over the same code.

Prefer
// eslint.config.js
import prettier from
  "eslint-config-prettier";

export default [
  js.configs.recommended,
  prettier,
];
Avoid
// ESLint stylistic rules and
// Prettier both rewrite the
// same lines and disagree

Enforcing formatting

Check formatting in CI so unformatted code cannot merge. A pre-commit hook catches it earlier still.

Prefer
# CI
npx prettier --check .

# before commit
npx prettier --write .
Avoid
# no check at all, so
# formatting drifts and
# diffs fill with noise

FAQ

Frequently asked questions

Keep learning

Related topics from the roadmap.

$ start learning

Ready to start learning Prettier?

Our interactive tutorial walks you through Prettier step by step — with quizzes and real code you can run in the browser.