Sub topic 10
Ignoring Files and Code Blocks
Prettier formats every supported file it finds. That is usually what you want, but it also means Prettier will rewrite build output, generated code, vendored libraries, and hand-aligned tables unless you tell it not to. This chapter covers the two ways to exclude things: ignore files, which exclude whole paths, and ignore comments, which exclude a single piece of code.
This chapter assumes you have Prettier installed and can run npx prettier . --check, as covered in the earlier Prettier chapters. It also assumes you know how .gitignore patterns work.
Two levels of ignoring
Prettier gives you two separate tools for this:
| Mechanism | Scope | Where it lives | Typical use |
|---|---|---|---|
| `.prettierignore` | Whole files and folders | Project root | Build output, generated code, lockfiles |
| `.gitignore` (read automatically) | Whole files and folders | Project root | Anything Git already ignores |
| `// prettier-ignore` comment | The next syntax node only | Inside a source file | Matrices, aligned tables, ASCII art |
| `--ignore-path` flag | Replaces the default ignore files | Command line | Monorepos, custom ignore locations |
Use the file-level tools for most cases. Use ignore comments only for small, deliberate exceptions.
How Prettier decides whether to format a file
Prettier always skips node_modules unless you pass --with-node-modules, and it skips version control folders such as .git. Since Prettier 3, it also reads both .gitignore and .prettierignore from the current working directory by default.
Writing a .prettierignore file
.prettierignore uses the same syntax as .gitignore. Create it next to package.json:
# Build output
dist/
build/
coverage/
# Generated code
src/generated/
*.min.js
# Lockfiles are written by npm, not by people
package-lock.json
# Re-include one file from an ignored pattern
*.snap
!src/__snapshots__/keep-formatted.snapA few rules follow from the gitignore syntax:
- A trailing slash (
dist/) matches only directories. - A pattern without a slash (
*.min.js) matches at any depth. - A leading slash (
/config.js) anchors the pattern to the folder containing the ignore file. !re-includes a file that an earlier pattern excluded.
You do not need to repeat .gitignore
Prettier 3 reads .gitignore automatically, so dist/ and coverage/ may already be covered. List only paths that are tracked in Git but should not be formatted, such as generated API clients, vendored scripts, snapshot files, and lockfiles. Keeping .prettierignore short makes each entry deliberate.
Checking whether a file is ignored
When a file isn't being formatted and you don't know why, ask Prettier directly:
npx prettier --file-info src/generated/client.jsThe output is JSON:
{ "ignored": true, "inferredParser": "babel" }If ignored is true, an ignore file matched the path. If inferredParser is null, Prettier does not know how to parse that extension, so it would skip the file even without an ignore entry.
Custom ignore paths
The --ignore-path flag replaces the default list. You can pass it more than once:
npx prettier . --check --ignore-path .gitignore --ignore-path config/.prettierignorePassing --ignore-path turns off the default behavior completely. If you pass only config/.prettierignore, Prettier stops reading .gitignore. This catches people out in monorepos, where a package-level script suddenly starts formatting build output.
Ignoring code inside a file
A prettier-ignore comment tells Prettier to print the next syntax node exactly as written. A syntax node is one complete piece of the parsed code: a statement, an expression, an object property, a class member, or a JSX element.
// prettier-ignore
const identity = [
1, 0, 0,
0, 1, 0,
0, 0, 1,
];
const config = {
// prettier-ignore
keymap: { up: 'k', down: 'j', left: 'h', right: 'l' },
timeout: 5000,
};
class Grid {
// prettier-ignore
static OFFSETS = [[-1, 0], [1, 0],
[0, -1], [0, 1]];
size = 3;
}
console.log(identity.length, config.timeout, Grid.OFFSETS.length);In this example only identity, the keymap property, and the OFFSETS member keep their manual layout. Prettier still formats timeout, size, and the console.log call.
Comment syntax per language
The comment has to be valid in the language you are writing:
| Language | Ignore comment |
|---|---|
| JavaScript, TypeScript | `// prettier-ignore` |
| JSX children | `{/* prettier-ignore */}` |
| CSS, SCSS, Less | `/* prettier-ignore */` |
| YAML | `# prettier-ignore` |
| GraphQL | `# prettier-ignore` |
In JSX, put the comment as a child expression directly before the element:
export function Board() {
return (
<div>
{/* prettier-ignore */}
<span className="cell" data-x="0" data-y="0" />
<span className="cell" data-x="1" data-y="0" />
</div>
);
}Markdown is the only common format that supports range ignores. This makes it useful for generated sections such as tables of contents:
<!-- prettier-ignore-start -->
| Name | Value |
|-|-|
| a | 1 |
<!-- prettier-ignore-end -->Range ignores work only at the top level of a Markdown document. JavaScript has no range form, so to exclude a large region you must either ignore each node or move the code into its own file and add that file to .prettierignore.
Common mistakes with ignore comments
- Wrong placement. The comment must come directly before the node it applies to. In
const x = // prettier-ignorefollowed by an array on the next line, the comment attaches to the array expression, and the result can be surprising. Put the comment above the whole statement. - Expecting file-wide effect. A
// prettier-ignoreat the top of a file affects only the first statement. JavaScript has no ignore pragma for a whole file, so use.prettierignoreinstead. - Confusing it with
requirePragma. TherequirePragmaoption works the other way round: Prettier formats only files that start with an@formator@prettierdocblock comment. It helps when you adopt Prettier gradually in an old codebase, but it does not ignore anything. - Overuse. Every ignore comment is a spot where the code style is now enforced by a person instead of a tool. If you find many of them, change an option such as
printWidth, or move the code to a data file.
ESLint ignores are separate
Prettier's ignore files have no effect on ESLint, and the reverse is also true. In flat config, you give ESLint global ignores with a config object whose only key is ignores:
// eslint.config.js
import js from "@eslint/js";
export default [
{ ignores: ["dist/", "coverage/", "src/generated/**"] },
js.configs.recommended,
];ESLint ignores node_modules and .git by default. Unlike Prettier 3, it does not read .gitignore on its own. Flat config also does not support .eslintignore; the migration chapter covers moving those entries across.
Ignores and lint-staged
lint-staged passes staged file paths to Prettier one by one. Prettier still applies .prettierignore to paths given this way, so ignored files are skipped without an error. If you ever see "No files matching the pattern were found" in a hook, an ignore pattern probably matched every staged file in that batch. Add --ignore-unknown to the Prettier command so files with no parser are skipped quietly too:
{
"lint-staged": {
"*": "prettier --write --ignore-unknown"
}
}Exercise
Your .prettierignore contains build/ followed by !build/manifest.json, but Prettier still skips build/manifest.json. Why, and how do you fix it?
Show answer
Gitignore-style matching never looks inside an excluded directory, so the negation can't re-include a file under it. Replace build/ with build/* (which ignores the folder's contents, not the folder) and keep !build/manifest.json.
Exercise
A teammate adds "format": "prettier . --write --ignore-path .prettierignore" and Prettier starts formatting files in a Git-ignored tmp/ folder. What changed?
Show answer
Passing --ignore-path replaces Prettier's default list of .gitignore and .prettierignore. Only .prettierignore is now read, so paths ignored only in .gitignore get formatted. Either remove the flag or add --ignore-path .gitignore as well.
Exercise
You put // prettier-ignore on line 1 of a 200-line generated file, expecting Prettier to leave the whole file alone. What actually happens, and what should you do instead?
Show answer
The comment applies only to the first syntax node (the first statement), so Prettier formats the rest of the file. Add the file's path or folder to .prettierignore, and run npx prettier --file-info <path> to confirm that "ignored" is true.