Sub topic 9
Configuring Prettier
This chapter assumes you have already installed Prettier and run it from the command line. You should also be comfortable editing JSON files. Prettier is built to need very little configuration. The few options it has are mostly about style, and teams usually settle on them once and then leave them alone.
Where Prettier Looks for Settings
Prettier looks for a configuration file starting in the directory of the file it is formatting, then moves up through each parent directory. It uses the first one it finds. Recognized names include .prettierrc (JSON or YAML), .prettierrc.json, .prettierrc.yaml, prettier.config.js or .mjs, and a "prettier" key in package.json.
To check which file applies, run npx prettier --find-config-path src/index.js.
A Small Config File
Most projects set only a few options and accept the defaults for the rest:
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100
}The Options You Will Actually Change
| Option | Default (Prettier 3) | Effect |
|---|---|---|
| `semi` | `true` | Adds semicolons at the end of statements |
| `singleQuote` | `false` | Uses `'` instead of `"` for strings |
| `trailingComma` | `"all"` | Adds trailing commas wherever they are valid |
| `printWidth` | `80` | The line length Prettier tries to stay within |
| `tabWidth` | `2` | Number of spaces per indentation level |
| `useTabs` | `false` | Indents with tabs instead of spaces |
| `arrowParens` | `"always"` | Writes `(x) => x` instead of `x => x` |
| `endOfLine` | `"lf"` | Sets the line-ending style |
Per-File Overrides
The overrides key applies different options to files that match a glob pattern:
{
"singleQuote": true,
"overrides": [
{
"files": "*.md",
"options": { "printWidth": 80, "proseWrap": "always" }
}
]
}Common Mistakes
- Treating
printWidthas a hard limit. It is a target. Prettier will not break a long string or a long import path to fit it. - Leaving settings to each editor. If no project config file exists, editor-level settings take over and developers get different output. Commit the config file.
- Adding comments to
.prettierrc.json. JSON does not allow comments, so Prettier fails to parse the file. Use YAML orprettier.config.mjsif you need comments. - Mixing Prettier versions. Defaults change between major versions. For example,
trailingCommachanged from"es5"to"all"in version 3. Pin Prettier indevDependencies.
Exercise
Your repository root has a .prettierrc with "singleQuote": true. A teammate adds packages/api/.prettierrc containing only { "semi": false }. Which quote style will files in packages/api/ use, and why?
Show answer
Double quotes. Prettier uses only the nearest config file and does not merge it with files higher up. The nested file does not set singleQuote, so the default false applies. To fix this, set singleQuote in the nested file too, or delete the nested file and use overrides in the root config.
Exercise
Write a .prettierrc that uses tabs for indentation and no semicolons, and applies "tabWidth": 4 only to *.yaml files.
Show answer
{"useTabs": true, "semi": false, "overrides": [{"files": "*.yaml", "options": {"tabWidth": 4}}]}