Prettier: check and write
Prettier is a formatter: it parses a file and prints it again in one style, and it has almost nothing to configure. The linter asks whether the program is right; the formatter decides where the spaces go and never changes what the code does, which is also why a team can stop arguing about the second question on the day it is installed.
The lesson formats a file with three style mistakes in it, checks the whole folder with --check, rewrites it with --write and runs the check again. The last section puts the two tools on the same two files and shows that each one is blind to the problem of the other.
The install and the config
Prettier is a development tool, so it goes into devDependencies. The config is a small JSON file at the root of the project, and the two options below are the ones worth deciding: the quote style and the width after which the formatter breaks a line.
npm install --save-dev prettier@3.9.10
npx prettier --version
3.9.10
The file .prettierrc.json:
vi .prettierrc.json
{
"singleQuote": true,
"printWidth": 80
}
Without the file Prettier uses double quotes and a width of 80, so the first option is the only one that changes the default here. The other options it has are few on purpose: a formatter with a hundred knobs is a second language for the team to learn.
prettier --check
--check reads the files, compares the result with the text on disk and lists the ones that differ. It writes nothing, and it exits with 1 when the list is not empty, which makes it the formatter's version of a lint run.
The file src/messy.js:
const items = [ "one","two","three" ]
function join(list,separator){
return list.join(separator)
}
console.log( join(items,"-") )
Four spaces where the project uses two, no semicolons, no space after a comma, and a call with a space inside its parentheses. None of that is a bug, and the linter of the previous lessons has nothing to say about the file.
npx prettier --check .
Checking formatting...
[warn] src/messy.js
[warn] Code style issues found in the above file. Run Prettier with --write to fix.
One line per file and a summary, and the status is 1. The command also read package.json and the config file, which are already in the style, so they do not appear. The [warn] prefix is Prettier's own, and the word after it is what a reader types next.
prettier --write
--write does the same work and saves the result. Its output is one line per file with the time the formatting took, which changes on every run, so the command below silences the lines that succeeded and keeps only the problems:
npx prettier --write . --log-level warn
A run that fixes everything prints nothing and exits with 0. The file is now in the style of the config, and every change is one a reader would have made by hand.
cat src/messy.js
const items = ['one', 'two', 'three'];
function join(list, separator) {
return list.join(separator);
}
console.log(join(items, '-'));
Double quotes became single ones, the indentation went to two spaces, the commas and the parentheses got their spaces and the semicolons appeared. The blank line after the array stayed, because Prettier keeps the empty lines a writer puts between blocks and normalises only the ones inside a block.
npx prettier --check .
Checking formatting...
All matched files use Prettier code style!
The second check costs a second and answers the only question a pipeline asks. A file that is generated, such as a lock file, is formatted too; a file that a formatter must not touch goes into .prettierignore, which has the same syntax as a .gitignore.
Prettier and ESLint together
The two tools overlap in one place and nowhere else. ESLint used to have rules about indentation and quotes; those were deprecated, because a formatter does that job better and the two disagreed on every second file. What is left in the recommended set is about correctness, and Prettier has no opinion about correctness at all: it formats a file with an unused variable exactly as happily as one without.
One file makes the split visible. It is written in the style of the config and has one mistake that only the linter can see.
The file src/unused.js:
vi src/unused.js
const unused = 1;
console.log('a line that is fine');
npx eslint --format json src/unused.js | node summarise.js
npx prettier --check src/unused.js
rm src/unused.js
unused.js: 1 error(s), 0 warning(s)
1:7 no-unused-vars - 'unused' is assigned a value but never used.
Checking formatting...
All matched files use Prettier code style!
The linter reports the variable and the formatter approves the whole file. Turn the pair around and the same thing happens with the file of the first section: Prettier lists it, ESLint says nothing. A project runs both, in that order, and the lesson after the next puts them into one command.
Exercises
- Remove the config file and run --check again. Which quotes does Prettier want now, and how many files differ?
- Set printWidth to 40 and run --write. Which line breaks first, and what does the change cost the reader?
- Write a line with a very long string and format it. What does Prettier do with a string it cannot break, and why does it not split the literal?
- Add *.json to .prettierignore and run --check .. Which file disappears from the output, and who formats it now?
- Run npx prettier src/messy.js without a flag. Where does the result go, and how would a script write it to another file?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
prettier --checkdo to a file that is not in the style?Answer
It lists the file and exits with 1
The check writes nothing; only --write saves the result.
-
What does this program print?
const text = " a b "; console.log(text.trim().split(/\s+/).length);Answer
2
The trim removes the ends and the split turns the two words into two entries.
-
Which question does a formatter answer?
Answer
Where the spaces and the line breaks go
The formatter changes the text and never the meaning, and correctness belongs to the linter and the tests.
-
What does this program print?
const before = "const a = 'x'"; console.log(before.includes('"'));Answer
false
The value is written with single quotes, so no double quote appears in the string.
-
What does
prettier --writeprint for a file it formatted successfully?Answer
A line with the file name and the time, which
--log-level warnhidesThe default level reports each file with its timing, and the warn level keeps only problems.
-
What does this program print?
const config = { singleQuote: true, printWidth: 80 }; console.log(Object.values(config).join(" "));Answer
true 80
The values of the two keys are kept in the order of the object.
-
Why were the formatting rules removed from ESLint?
Answer
A formatter does that job better, and the two tools disagreed
The split gives each tool one job: the linter for correctness and the formatter for the text.
-
What does this program print?
const files = ["src/messy.js", "package.json"]; const js = files.filter((file) => file.endsWith(".js")); console.log(js.length, js[0]);Answer
1 src/messy.js
One entry ends with the extension, and it is the first of the list.
-
What does a file in
.prettierignoreget?Answer
No formatting from Prettier at all
The ignore file removes the path from every run, the same way a .gitignore file does for a repository.
-
What does this program print?
const width = 80; const line = "x".repeat(width + 1); console.log(line.length > width);Answer
true
The line is one character longer than the width, so the comparison is true.
The programs and commands on this page were run with Node.js 24.22.1, Prettier 3.9.10 and ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev