How to check prettier formatting

Run npx prettier --check . in the project root. It prints one [warn] line per file whose formatting differs from what Prettier would write, then a count, and exits 1. A tree that already matches prints All matched files use Prettier code style! and exits 0. Nothing on disk changes.

Why check this

Formatting is a gate in the pull request job, next to the lint and type checks. Its value is not tidiness, it is diff size: one reformatted file turns a three-line change into a three-hundred-line review, and the real edit hides inside it.

The failure this catches on Windows is different, and it is the one that wastes an afternoon. A check fails on a file nobody in the team has opened, because the working copy has CRLF line endings and Prettier writes LF. The tool reports a formatting problem, the developer opens the file, and the two versions look identical on screen.

Prerequisites

// src/price.js, formatted by Prettier, LF endings
export function price(cents, currency) {
  return new Intl.NumberFormat("en-US", { style: "currency", currency }).format(
    cents / 100,
  );
}
// src/tag.js, spacing and quotes that Prettier disagrees with
export function tag( name ,value ){
    return name+':'+value
}
node -e "const fs=require('fs');fs.writeFileSync('src/legacy.js',fs.readFileSync('src/price.js','utf8').replace(/\n/g,'\r\n'))"

Steps

  1. Step 1.

    Read the Prettier version the project resolves to. Output rules change between major versions, so a result without a version is not comparable.

    npx prettier --version
    
    3.9.6
  2. Step 2.

    Check the tree and read the exit code.

    npx prettier --check . --no-color; echo "exit=$?"
    
    Checking formatting...
    [warn] src/legacy.js
    [warn] src/tag.js
    [warn] Code style issues found in 2 files. Run Prettier with --write to fix.
    exit=1

    Two files, one count line, exit 1. --no-color drops the escape sequences, which keeps a CI log readable.

  3. Step 3.

    Ask what Prettier would write for one of them. --check names files and never shows the difference.

    npx prettier src/tag.js --no-color
    
    export function tag(name, value) {
    return name + ":" + value;
    }

    With no --write and no --check, the formatted result goes to stdout and the file is untouched. Compare it with the original by eye, or pipe it into diff.

  4. Step 4.

    Compare the other flagged file with the one that passed.

    node -e "const fs=require('fs');const a=fs.readFileSync('src/legacy.js','utf8'),b=fs.readFileSync('src/price.js','utf8');console.log('same text:',a.replace(/\r/g,'')===b,'| CR bytes:',(a.match(/\r/g)||[]).length)"
    
    same text: true | CR bytes: 5

    Remove the carriage returns and the two files are the same string. The only difference Prettier found is five bytes no editor displays.

  5. Step 5.

    Prove it by running the same check with CRLF accepted.

    npx prettier --check src/legacy.js --no-color --end-of-line crlf; echo "exit=$?"
    
    Checking formatting...
    All matched files use Prettier code style!
    exit=0

    Same file, same content, opposite verdict. The failure in step 2 was the endOfLine default, not the code.

  6. Step 6.

    Read the Git setting that produces those endings on Windows.

    git config --get core.autocrlf
    
    true

    true means Git converts LF to CRLF on checkout. Every file in the working copy then disagrees with the Prettier default, whether or not anyone edited it.

How to read the result

| What you see | What it means | What to do | | --- | --- | --- | | All matched files use Prettier code style!, exit 0 | Every file Prettier matched is formatted | Nothing. Record the version from step 1. | | [warn] lines and exit 1 | Those files differ from Prettier output | Run npx prettier --write on them and review the diff before committing. | | A file you never touched is flagged | Line endings, encoding or a version bump | Run step 4 on it. If the text matches after removing \r, the cause is CRLF. | | Every file is flagged after an upgrade | A default changed in a Prettier major | Reformat the tree in one commit of its own, so the next review has a clean diff. | | [error] No files matching the pattern were found and exit 2 | The path argument matched nothing | Check the working directory. Exit 2 is a usage failure, not a formatting result. |

Common mistakes

Sign: The check fails on files nobody edited, and the file looks correct in the editor.Cause: Git on Windows with `core.autocrlf=true` writes CRLF into the working copy, and Prettier compares against an `endOfLine` default of `lf`. Step 4 shows `src/legacy.js` is the same string as a passing file once the five carriage returns are removed, and step 5 makes it pass by changing one flag. Add `* text=auto eol=lf` to `.gitattributes` and normalise the checkout, rather than changing the Prettier option per machine.
Sign: CI and a developer machine disagree about the same commit.Cause: A Linux runner checks out LF and a Windows laptop checks out CRLF from the same repository. Nothing in the commit differs. Until `.gitattributes` fixes the checkout, the two runs are reading different bytes, and no amount of reformatting makes both pass.
Sign: `--check` is used as a diff and reports nothing useful.Cause: `--check` prints file names only, by design. Step 3 is how you see the change Prettier wants: run it with no flag and read stdout. A team that pipes `--check` output into a review comment ends up quoting a list of paths.
Sign: Formatting rules and lint rules fight over the same line.Cause: A lint rule about quotes or spacing and a formatter option will both rewrite the line, in two different runs, and the second undoes the first. Keep formatting in the formatter, turn the stylistic lint rules off, and run both commands in the same job.

What to check next

FAQ

How do I check formatting without changing files?

npx prettier --check . reads and reports. It writes nothing. --write is the flag that edits, and --list-different prints the same file list as --check with no summary line.

Why is a file flagged when the code looks identical?

Its line endings differ. Step 4 removes the carriage returns and the two files match. Prettier defaults endOfLine to lf, so a CRLF working copy fails on every file it contains.

Should I set endOfLine to auto?

It hides the problem rather than fixing it. --end-of-line auto on src/legacy.js exits 0, and so does the CRLF file that Git will commit next. Fix the checkout with .gitattributes and keep the default.

What exit code does prettier return?

0 when every matched file is formatted, 1 when at least one is not, and 2 when the invocation itself failed, such as a path that matches no file. Treat 2 as a broken command rather than a formatting result.

Which files does the check cover?

Every file under the path whose extension Prettier has a parser for, minus node_modules and anything in .prettierignore. That includes JSON, Markdown and CSS, so a formatting job touches more than the source tree.

Verified

Verified by Maks Vernyprettier 3.9.6node 22.23.2git 2.41.0.windows.2

Each output block is what the command above it printed on that date, on the host named in the step. Figures read from a live site move between runs. Compare the shape of the answer rather than the digits, and see the methodology for how a page is re-verified.

basic6 minpublished updated Maks Verny