skip to content

In Prettier, how is the `.prettierrc` that applies to a file found, and how do `overrides` give some files different options?

level: middleimportance: should knowfreq 12%

answer

  1. start at the formatted file
  2. walk up, stop at first
  3. no global config
  4. files required, excludeFiles optional
  5. parser only inside overrides

basics

~20 s

Prettier searches from the formatted file's directory upward and uses the first config it finds; there is no global config. Inside it, overrides entries match files globs, optionally minus excludeFiles, and apply their own options.

solid answer

~40 s

For each file, Prettier starts in that file's directory and walks up the tree until it finds a configuration: a `"prettier"` key in `package.json`, a `.prettierrc` in JSON or YAML, `.prettierrc.json` / `.yaml` / `.json5` / `.toml`, or a JavaScript or TypeScript file such as `prettier.config.mjs`. The first one found is used; Prettier intentionally has no global configuration, so a project formats the same on every machine. Within that file, `overrides` is an array of entries, each with a required `files` glob (string or array), an optional `excludeFiles`, and an `options` object applied to matching files on top of the top-level options. `.editorconfig` values are converted and are overridden by the Prettier config. `parser` belongs only inside `overrides`; at the top level it forces one parser on every file.

go deeper

for a junior

Recall that a .prettierrc near the code configures Prettier and that there is no global config. Know the overrides shape: files plus options.

for a middle

Explain the upward search that stops at the first file, the files and excludeFiles keys, and why parser belongs only inside an override.

for a senior

Debug a package that formats differently from the root: a shadowing nested config, CLI flags, or .editorconfig values, using --find-config-path.

for a principal

Choose between one root config with overrides and per-package configs in a monorepo, weighing shared defaults against teams drifting apart.

## Where Prettier looks Prettier resolves configuration **per file**. The docs: "The configuration file will be resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn't) found." The search stops at the first configuration it finds; a parent folder's file is not merged in. The accepted forms, in the order the docs list them: - a `"prettier"` key in `package.json` (or `package.yaml`); - `.prettierrc`, written in JSON or YAML; - `.prettierrc.json`, `.prettierrc.yml`, `.prettierrc.yaml`, `.prettierrc.json5`; - `.prettierrc.js` / `prettier.config.js`, plus the `.mjs`, `.cjs`, `.ts`, `.mts` and `.cts` variants, exporting an object; - `.prettierrc.toml`. ## No global configuration "Prettier intentionally doesn't support any kind of global configuration." The reason is reproducibility: copy the project to another computer and it must format identically. So there is no per-user file that silently changes output. If no file is found, Prettier uses its defaults, adjusted by any `.editorconfig` values. Related CLI controls: | Need | Command | |---|---| | See which file applies to a path | `prettier --find-config-path path/to/file.ts` | | Use a file Prettier would not find, e.g. in `config/` | `prettier --config config/.prettierrc . --check` | | Ignore any config file and use defaults | `prettier --no-config` | | Decide whether CLI flags or the file win | `--config-precedence` (default `cli-override`) | ## `overrides`: different options for some files One file can still treat parts of the project differently. `overrides` is an array; each entry has: 1. **`files`** - required, a glob string or an array of globs; 2. **`excludeFiles`** - optional, globs removed from that match; 3. **`options`** - the options applied to matching files, on top of the top-level ones. For the coworking portal's repository, Markdown docs wrap prose to the print width, a legacy folder keeps four-space indentation, and the vendored bundle is excluded from that rule: ```json { "singleQuote": true, "overrides": [ { "files": "*.md", "options": { "proseWrap": "always" } }, { "files": "src/legacy/**/*.js", "excludeFiles": "src/legacy/vendor/**", "options": { "tabWidth": 4 } } ] } ``` An override changes the options used for matching files; it is not an ignore mechanism. Skipping files is the job of `.prettierignore`. ## The `parser` trap Prettier infers the parser from the file extension. The docs say **never** to put `parser` at the top level: doing so "effectively disable[s] Prettier's automatic file extension based parser inference" and forces that parser onto every file, CSS included. Use it only inside an override, for example to format an extension-less `.prettierrc` with `{ "files": ".prettierrc", "options": { "parser": "json" } }`. ## `.editorconfig` and nested configs - If a project has an `.editorconfig`, Prettier converts `indent_style`, `indent_size`, `max_line_length` and `end_of_line` into its options. A Prettier config **overrides** them, and the `.editorconfig` search stops at the project root. `--no-editorconfig` turns this off. - A package with its own `.prettierrc` deeper in a monorepo gets **only** that file, because the upward search stops there. A root `semi: false` does not reach `packages/billing/` if `packages/billing/.prettierrc` exists and omits `semi`; that package then gets the default `semi: true`. ## Sharing one configuration across packages Because configuration files do not cascade, the documented way to reuse settings is a **shareable config**: an npm package that exports a regular Prettier configuration object. A project then points at it by name, either as the string value of the `"prettier"` key in `package.json` or as the whole content of a `.prettierrc`, for example `"@deskbook/prettier-config"`. To change a few options, import the shared object in a `prettier.config.mjs`, spread it and override the properties you need. In a monorepo that gives every package the same base without relying on a parent file being merged, which never happens. | Approach | Good for | Watch out for | |---|---|---| | One root file with `overrides` | one team, a few special folders | a nested file silently shadows it | | Shareable config package | many packages or repositories | publishing and versioning the package | | Per-package files | packages owned by different teams | settings drifting apart | ## Checklist when output differs between machines - Run `prettier --find-config-path` on the file in question on both machines. - Check whether one machine passes CLI flags that override the file. - Check for a nested config that shadows the root one. - Check `.editorconfig` values that apply when no Prettier option is set.

  • A root `.prettierrc` sets `semi: false`, and `packages/billing/.prettierrc` sets only `singleQuote: true`. What does a file in `packages/billing/src` get?
    Only the package's file applies, because the upward search stops at the first configuration found. The file gets single quotes and the default `semi: true`. To share the root settings, repeat them or import them in a JavaScript config.
  • When would you use `--config` instead of relying on the search?
    When the configuration lives somewhere the upward search never reaches, such as a `config/` folder, or to skip the lookup cost when formatting many single files from a script. `--find-config-path` prints the file the search would pick so you can pass it.

  • Prettier merges every .prettierrc from the file up to the root
  • A user-level global Prettier config applies to every project
  • Listing a folder in overrides excludes it from formatting
  • Setting parser at the top level is a harmless default
  • Prettier reads its config from the working directory, not the file