What Is Prettier? How Opinionated Code Formatting Works

Learn what Prettier does, how print width and its few options shape your code, how it differs from a linter, and how to set it up in an editor, a repo and CI.

Updated September 29, 2026 5 min read

Use the free Code Formatter (Prettier)Format JS, TS, CSS, HTML, Markdown, YAML, JSON and GraphQL code

Code reviews that argue about tabs, quote marks and where a line should break waste everyone's time. So do diffs where half the changed lines are someone's editor reformatting a file. Prettier was created to end those debates: it takes your code, throws away the original formatting and prints it again according to one consistent style.

This guide explains how an opinionated formatter works, what the few configurable options actually change, where Prettier stops and a linter begins, and how to use it both for a quick one-off clean-up and as a permanent part of a project.

How Prettier formats code

Most "beautifiers" adjust whitespace in your existing text. Prettier works differently:

  1. It parses your code into a syntax tree, the same structure a compiler sees.
  2. It discards almost all original formatting, keeping only things that carry meaning, such as comments and some intentional blank lines.
  3. It prints the tree again, choosing line breaks so that each line fits within the print width.

Because the output depends on the syntax tree rather than on how the code was typed, two developers with different habits get identical results. It also means Prettier cannot format code that does not parse: a missing bracket stops it with an error that points to the line.

Here is a compressed snippet before formatting:

const invoice={id:"INV-1042",lines:[{sku:"A-1",qty:2,price:9.5},{sku:"B-7",qty:1,price:120}],notes:'Net 30'}
function total(inv){return inv.lines.reduce((sum,l)=>sum+l.qty*l.price,0)}

And after Prettier with default options:

const invoice = {
  id: "INV-1042",
  lines: [
    { sku: "A-1", qty: 2, price: 9.5 },
    { sku: "B-7", qty: 1, price: 120 },
  ],
  notes: "Net 30",
};
function total(inv) {
  return inv.lines.reduce((sum, l) => sum + l.qty * l.price, 0);
}

Quotes were normalised, semicolons added, the object was broken across lines because it did not fit in 80 characters, and a trailing comma was added after the last array item.

The options that matter

Prettier deliberately offers few options. The important ones:

Option Default Effect
Print width 80 The line length Prettier tries to stay within. It is a target, not a hard limit.
Tab width / use tabs 2 spaces Indentation size, or tabs instead of spaces.
Semicolons on Add semicolons at statement ends, or only where required to avoid ambiguity.
Single quotes off Prefer ' over " in JavaScript, CSS and YAML strings.
Trailing commas all Add commas after the last item in multi-line lists (all, es5 or none).

A common misunderstanding is that print width is a maximum. Prettier will exceed it when a single long string or identifier cannot be broken, and it will join short lines that you split by hand if they fit. Trailing commas look odd at first, but they make diffs cleaner: adding an item changes one line instead of two.

Format code in your browser

The Code Formatter runs Prettier in your browser, with nothing uploaded, which is handy for a snippet from a ticket, a config file on a server, or a machine where you cannot install Node.js.

  1. Open Code Formatter and pick the Language: JavaScript / JSX, TypeScript / TSX, JSON, CSS, SCSS, Less, HTML, Markdown, YAML or GraphQL.
  2. Paste your code, use Open file, or click Load sample to see messy example code for that language.
  3. Set Print width (20 to 200) and Indent (2 spaces, 4 spaces or Tabs).
  4. For JavaScript and TypeScript, choose Trailing commas (All (default), ES5 or None) and toggle Semicolons. Single quotes is available for JavaScript, TypeScript, CSS, SCSS, Less and YAML.
  5. Read the Formatted code. If the input has a syntax error, the message shows the line and column.
  6. Copy the result or Download it with the matching file extension.

For a quick look at messy SQL or XML, which Prettier does not handle in this tool, use the SQL Formatter or XML Formatter instead.

Prettier in a real project

For a codebase, install Prettier as a development dependency and pin the version so everyone gets identical output:

npm install --save-dev --save-exact prettier

Add a config file named .prettierrc in the project root. Keep it short; every option you change is one more thing newcomers must learn:

{
  "printWidth": 100,
  "singleQuote": true
}

Then format and check from the command line:

# Rewrite files in place
npx prettier . --write

# Report unformatted files without changing them (useful in CI)
npx prettier . --check

A .prettierignore file, with the same syntax as .gitignore, keeps generated folders such as dist/ out of it. Most editors have a Prettier extension that formats on save, and many teams also run it in a pre-commit hook so unformatted code never reaches the repository.

Prettier versus a linter

Prettier decides how code looks. A linter such as ESLint decides whether code is risky: unused variables, missing await, accidental globals. They complement each other. The common setup is to let Prettier handle all formatting and turn off the linter's style rules that would conflict, so the two tools never fight over the same line. eslint-config-prettier is the widely used package for switching those rules off.

Troubleshooting

Prettier keeps undoing my manual line breaks

That is by design. If a statement fits within the print width, it is joined onto one line. Raise or lower the print width, or accept the result; the consistency is the point.

Formatting fails with "Unexpected token"

The code does not parse. Check the selected language first (TypeScript syntax will fail in JavaScript mode), then look for an unclosed bracket or quote at the reported line.

Results differ between machines

Different Prettier versions can format some constructs differently. Pin an exact version in package.json and make sure editors use the project's copy rather than a bundled one.

Only JSON needs fixing

For JSON-only work, the JSON Formatter adds validation and minification options. To compare two files after formatting, the Text Diff tool highlights the real changes.

Frequently asked questions

Is Prettier a linter?

No. Prettier only changes formatting and never reports logic problems. Use it together with a linter such as ESLint, with the linter's formatting rules turned off.

Why does Prettier add trailing commas?

Trailing commas mean that adding an item to a multi-line list changes only one line, which keeps diffs small. You can choose es5 or none if your target environment or team prefers.

What print width should I use?

The default of 80 suits most teams; 100 or 120 is common for code with long names. Pick one value for the whole project and store it in the config file.

Does the online formatter change what my code does?

No. Prettier only changes whitespace, line breaks, quote style, optional semicolons, parentheses and trailing commas, all of which leave behaviour unchanged.

Can Prettier format code with syntax errors?

No. It must parse the code first, so it stops and reports the location of the first syntax error instead.

Try it now: Code Formatter (Prettier)

Loading tool…