Sub topic 9
Configuring Prettier
Prettier is opinionated on purpose. It has a small set of options, and most teams change only a handful of them. This chapter covers where Prettier looks for configuration, what the options do, how to give different files different settings, and the mistakes that make Prettier seem to ignore your config.
It assumes you have already installed Prettier and run it from the command line, as shown in the previous chapter. You should also be comfortable editing JSON files and working with ES modules.
Why Prettier Has So Few Options
Prettier's maintainers keep the option list short. Every option invites a team debate, and the whole point of the tool is to end those debates. There is no setting for "put the opening brace on its own line" or "align object values in columns." Prettier parses your code into an abstract syntax tree (AST), throws away the original formatting, and prints the tree again under its own rules. The options only adjust a few points in that printing step.
So the right question is "which defaults do we have a real reason to change?" Asking "how do I make Prettier match our old style?" usually leads nowhere.
A Typical Configuration File
Here is a small .prettierrc that changes four settings and leaves everything else at the defaults:
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100
}Two of these values, semi: true and trailingComma: "all", already match Prettier 3's defaults. Writing them out anyway is a reasonable choice. It documents intent, and it keeps output stable if a future major version changes a default. In Prettier 2, for example, the trailingComma default was "es5".
The Options You Will Actually Touch
| Option | Default (Prettier 3) | What it controls |
|---|---|---|
| `printWidth` | `80` | The line length Prettier tries to stay within |
| `tabWidth` | `2` | Spaces per indentation level |
| `useTabs` | `false` | Indent with tabs instead of spaces |
| `semi` | `true` | Add semicolons at the end of statements |
| `singleQuote` | `false` | Prefer `'` over `"` for strings |
| `quoteProps` | `"as-needed"` | Quote object keys only when required |
| `jsxSingleQuote` | `false` | Use single quotes in JSX attributes |
| `trailingComma` | `"all"` | Trailing commas in multi-line lists: `"all"`, `"es5"`, or `"none"` |
| `bracketSpacing` | `true` | Put spaces inside object braces: `{ a }` versus `{a}` |
| `bracketSameLine` | `false` | Put the `>` of a multi-line JSX tag at the end of the last line |
| `arrowParens` | `"always"` | Parentheses around a single arrow function parameter |
| `endOfLine` | `"lf"` | Line ending style: `"lf"`, `"crlf"`, `"cr"`, or `"auto"` |
| `proseWrap` | `"preserve"` | Wrapping of Markdown prose |
Some of these options are easy to misread:
printWidthis a target, not a hard limit. Prettier will not break a long string literal or a long import path. A line can still go over the width when there is no legal place to break it. Raising the width to 120 or more makes Prettier join more code onto single lines, and that can make diffs harder to read.singleQuotegives way to escaping. If a string contains more single quotes than double quotes, Prettier uses double quotes so it doesn't have to add backslashes.semi: falseis safe. Prettier knows where automatic semicolon insertion is risky. In those places it adds a leading;, for example before a line that starts with(or[.endOfLine: "lf"matters on Windows. If Git converts line endings on checkout (core.autocrlf=true), every file can show up as unformatted. The fix is to make Git and Prettier agree, usually with* text=auto eol=lfin.gitattributes. Switching Prettier to"auto"hides the problem instead of fixing it.
Config File Formats
Prettier accepts several file names. Choose one per project:
- A
"prettier"key inpackage.json .prettierrc, written as JSON or YAML.prettierrc.json,.prettierrc.yaml,.prettierrc.yml,.prettierrc.json5,.prettierrc.toml.prettierrc.js,.prettierrc.mjs,.prettierrc.cjsprettier.config.js,prettier.config.mjs,prettier.config.cjs
Use a JavaScript config when you need comments, computed values, or editor autocompletion. With a JSDoc type annotation, editors can check option names:
// prettier.config.mjs
/** @type {import("prettier").Config} */
const config = {
singleQuote: true,
printWidth: 100,
overrides: [
{
files: ["*.md"],
options: { proseWrap: "always", printWidth: 80 },
},
{
files: ["legacy/**/*.js"],
options: { semi: false },
},
],
};
export default config;The .mjs extension makes Node.js treat the file as an ES module whatever the "type" setting in package.json says. A plain prettier.config.js that uses export default needs "type": "module".
Overrides
The overrides array applies different options to files that match glob patterns. Each entry has files and, optionally, excludeFiles, both resolved relative to the config file. When several overrides match a file, they apply in order and later entries win. The top-level options form the base underneath them.
Common uses for overrides:
- Wider or narrower
printWidthfor Markdown or generated fixtures tabWidth: 4for a language whose community uses 4- Pinning a
parserfor files with unusual extensions, such as{ "files": "*.code-workspace", "options": { "parser": "json" } }
How Prettier Finds Its Config
ESLint's flat config has one config file at the project root. Prettier works differently. It resolves configuration separately for each file it formats. It starts in the file's own directory and walks up toward the file system root, and it stops at the first config file it finds.
Prettier does not merge config files. If packages/web/.prettierrc exists, then files under packages/web ignore the root .prettierrc completely. In a monorepo, people often add a nested config "just to change one option" and silently lose every other setting.
To share settings across packages, use one root config with overrides. You can also publish a shared config package and refer to it by name, for example "prettier": "@acme/prettier-config" in package.json. A JavaScript config can import that package and spread it.
When output looks wrong, ask Prettier which file it used:
npx prettier --find-config-path packages/web/src/index.jsEditorConfig Interaction
If the project has an .editorconfig file, Prettier reads a few of its properties: indent_style, indent_size or tab_width, max_line_length, and end_of_line. It maps them to useTabs, tabWidth, printWidth, and endOfLine. Any value set in the Prettier config takes precedence. This matters when an .editorconfig written for another tool contains max_line_length = 120. Prettier will use 120 unless your config says otherwise. The CLI flag --no-editorconfig turns this behavior off.
Common Errors
- Two config files at the same level. If a
"prettier"key inpackage.jsonand a.prettierrcsit in the same directory, only one of them is used. Keep a single source. - Editor and CLI disagree. The editor extension may be using its own bundled Prettier version or its own fallback settings. Install Prettier as a project dev dependency and pin the version, so the editor, scripts, and CI all format the same way.
- Upgrading changes output. Even with identical options, Prettier releases change formatting in small ways. Pin an exact version (
"prettier": "3.3.3", not^3.3.3), and upgrade in a dedicated commit that reformats the whole codebase.
Exercise
A monorepo has a root .prettierrc with {"singleQuote": true, "printWidth": 100}. A developer adds packages/api/.prettierrc containing {"semi": false}. Which options apply to packages/api/src/server.js, and how would you get the intended result?
Show answer
Prettier stops at the nearest config, packages/api/.prettierrc, and does not merge it with the root file. The file therefore gets semi: false with singleQuote: false and printWidth: 80 (the defaults), so the root settings are lost. Delete the nested file and add an override to the root config instead: "overrides": [{ "files": ["packages/api/**"], "options": { "semi": false } }].
Exercise
Your .prettierrc sets no printWidth, yet Prettier wraps lines at 120 characters. What is the most likely cause, and how do you confirm it?
Show answer
An .editorconfig file contains maxlinelength = 120, and Prettier maps it to printWidth. Confirm by running Prettier with --no-editorconfig and checking whether wrapping returns to 80. Then set printWidth explicitly in the Prettier config.