The site is on GitHub, and the first issue on the list asks for a formatter and a linter. Open src/lib/closing-soon.js, the previous developer’s unfinished notice, and you’ll see why. It looks like two people wrote it on different days without ever meeting. Some lines are indented by two spaces and others by four, and the quotes and semicolons are mixed too.
In this lesson, you’ll add Prettier to the project on a new branch, so the layout of every file follows the same rules. You’ll reformat the whole site in a commit of its own, check that every file passes, and set up VS Code to format each file when you save it. Start with the file that shows the problem best.
Add Prettier to the Project
Messy layout also makes changes harder to review. When two people format code differently, every change they make to the same file re-indents or re-quotes lines they didn’t mean to touch, and the diff hides the real change among them. Here are the first lines of closing-soon.js, as they appeared in the setup commit:
import { hoursFor, formatTime } from './hours.js'
// "Closing soon" notice for the home page. NOT FINISHED, don't use yet!!
// eslint-disable-next-line no-unused-vars
export function closingSoonNotice(date, leadMinutes) {
const today = hoursFor(date)
let minutesLeft
The import uses single quotes, while the rest of the project uses double quotes. The line with const today is indented by four spaces and the one after it by two, and neither line ends in a semicolon.
A formatter fixes this kind of mess for you. It reads your code and writes it back out with consistent indentation, quotes, semicolons, and linebreaks, and it never changes what the code does. Once a team lets one formatter decide the layout, nobody argues about style in a review anymore, and diffs show only real changes.
Every change goes through a pull request now, so the work for this issue starts on its own branch. Make sure you’re on main with a clean working tree, and then create the branch format-and-lint:
$ git switch -c format-and-lint
Switched to a new branch 'format-and-lint'
The branch is named for the whole issue, because ESLint joins Prettier on it in the next lesson.
Before you install Prettier, give it the same quick check as any new package. Open www.npmjs.com/package/prettier and look at the sidebar. The name is exactly prettier, the Weekly Downloads run into the millions, the Repository is prettier/prettier on GitHub, and the Last publish date is recent. The terminal confirms the repository:
$ npm view prettier repository.url
git+https://github.com/prettier/prettier.git
That’s the package you want, so install it with npm, using the same approach as for Vite:
$ npm install -D --save-exact prettier
added 1 package, and audited 18 packages in 656ms
9 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
The -D flag puts Prettier under devDependencies, because you need it while you work on the site, but the published pages never load it. The --save-exact flag is new. Look at what it did to package.json:
$ git diff package.json
diff --git a/package.json b/package.json
index e7b0d00..69afbdc 100644
--- a/package.json
+++ b/package.json
@@ -15,6 +15,7 @@
"dayjs": "^1.11.23"
},
"devDependencies": {
+ "prettier": "3.10.1",
"vite": "^8.3.8"
}
}
Vite’s version starts with a caret (^), which allows any newer 8.x release. Prettier’s version has no caret, so it’s pinned to exactly that one release. The lockfile already records which version you installed, but a later npm update could move a caret range forward.
That’s fine for most packages. For a formatter, though, a new release sometimes lays out code a little differently, and a pull request about holiday hours would suddenly come with a hundred reformatted lines.
Before Prettier changes anything, ask it which files it would change. The --check option only reports and doesn’t write any file, and npx runs the prettier command that you installed into node_modules/:
$ npx prettier --check .
npm notice run [email protected] npx
npm notice run 'prettier' --check .
Checking formatting...
[warn] deals.html
[warn] departments.html
[warn] index.html
[warn] pickup.html
[warn] README.md
[warn] scripts/check.js
[warn] src/data/deals.js
[warn] src/deals-page.js
[warn] src/lib/closing-soon.js
[warn] src/lib/hours.js
[warn] src/vendor/curbside.js
[warn] Code style issues found in 11 files. Run Prettier with --write to fix.
That’s more than the messy files. By default, Prettier aims for lines of at most 80 characters, although a long string can still exceed that width. The project’s tidy files, such as scripts/check.js, use lines up to 100 characters. The list also includes src/vendor/curbside.js, Generic Curbside’s widget. It’s their code, so you keep it exactly as they ship it.
Prettier’s options go in a file named .prettierrc in the project’s top folder. Create it with this content:
.prettierrc
{
"printWidth": 100
}
The files Prettier should leave alone go in .prettierignore, which works like .gitignore. Prettier already skips node_modules/ and everything that .gitignore lists, so .prettierignore only needs the vendor folder:
.prettierignore
# Generic Curbside's widget is their code, kept as they ship it
src/vendor/
Run the same check again, and the list gets shorter:
$ npx prettier --check .
npm notice run [email protected] npx
npm notice run 'prettier' --check .
Checking formatting...
[warn] deals.html
[warn] departments.html
[warn] index.html
[warn] pickup.html
[warn] README.md
[warn] src/lib/closing-soon.js
[warn] Code style issues found in 6 files. Run Prettier with --write to fix.
Now Prettier wants to change only the files that really are messy. It has few options on purpose, and a project usually sets one or two so the formatter fits the code it already has.
To make formatting one command that everyone on the project knows, add a format script to package.json. Open the file, put a comma after the check line, and add the new line below it:
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"check": "node scripts/check.js",
"format": "prettier --write ."
},
The --write option is the counterpart of --check. It rewrites every file that isn’t formatted yet. Last, the README’s file table is the first thing the next person on the site reads, so add a row for the two new files below the .gitignore row:
| `.prettierrc`, `.prettierignore` | Prettier's settings and its ignore list |
Check what Git sees before you stage anything:
$ git status
On branch format-and-lint
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
modified: package-lock.json
modified: package.json
Untracked files:
(use "git add <file>..." to include in what will be committed)
.prettierignore
.prettierrc
no changes added to commit (use "git add" and/or "git commit -a")
These five files are the whole setup. Stage them by name, keeping the lockfile together with package.json as always:
$ git add package.json package-lock.json .prettierrc .prettierignore README.md
Before you commit, run git diff --staged and read all of it, pressing Q to leave the pager. The lockfile’s part is the easiest to skim past, so here it is on its own, with the long integrity hash shortened:
$ git diff --staged package-lock.json
diff --git a/package-lock.json b/package-lock.json
index c914ea3..957bb5a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -11,6 +11,7 @@
"dayjs": "^1.11.23"
},
"devDependencies": {
+ "prettier": "3.10.1",
"vite": "^8.3.8"
}
},
@@ -780,6 +781,22 @@
"node": "^10 || ^12 || >=14"
}
},
+ "node_modules/prettier": {
+ "version": "3.10.1",
+ "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.10.1.tgz",
+ "integrity": "sha512-70vp…xmmXlg==",
+ "dev": true,
+ "license": "MIT",
+ "bin": {
+ "prettier": "bin/prettier.cjs"
+ },
+ "engines": {
+ "node": ">=14"
+ },
+ "funding": {
+ "url": "https://github.com/prettier/prettier?sponsor=1"
+ }
+ },
"node_modules/rolldown": {
"version": "1.2.19",
"resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.19.tgz",
The lockfile gained exactly one package entry, node_modules/prettier, at the version you pinned and downloaded from the npm registry. Prettier doesn’t depend on other packages, so any other new node_modules/ entry here would be a reason to stop and look.
As you read the rest of the diff, also check for anything that resembles a key, such as a curb_ value. This diff doesn’t contain one, so go ahead and commit. The message needs a body that says why you made the change, so run git commit without -m, and write the message in VS Code:
Add Prettier, pinned to one version
npm install -D --save-exact prettier, so every copy formats the
same way. .prettierrc sets the line width to 100 characters,
.prettierignore leaves Generic Curbside's widget as they ship it,
and npm run format formats the project.
Save and close the message’s tab, and Git makes the commit:
$ git commit
[format-and-lint 6510c28] Add Prettier, pinned to one version
5 files changed, 26 insertions(+), 1 deletion(-)
create mode 100644 .prettierignore
create mode 100644 .prettierrc
Your hash will be different, as always. Prettier is set up, but it hasn’t touched a single file yet, because the reformat gets a commit of its own.
Reformat in a Commit of Its Own
Picture the pull request a reviewer will read. If the new config and eighty reformatted lines arrive in one commit, the few lines that matter drown in the layout changes. When the reformat is in a commit of its own, a reviewer can skip it, because its message says it only changes layout. Run the new script:
$ npm run format
npm notice run [email protected] format
npm notice run prettier --write .
.prettierrc 36ms (unchanged)
css/styles.css 54ms (unchanged)
deals.html 41ms
departments.html 11ms
index.html 7ms
package-lock.json 22ms (unchanged)
package.json 1ms (unchanged)
pickup.html 8ms
README.md 67ms
scripts/check.js 146ms (unchanged)
...
src/lib/closing-soon.js 3ms
...
vite.config.js 2ms (unchanged)
Prettier lists every file it read, and the ones without (unchanged) are the ones it rewrote. The times differ on every run. Before you stage anything, look at the size of the change:
$ git diff --stat
README.md | 42 +++++++++++++++++++++---------------------
deals.html | 18 +++++++++---------
departments.html | 24 +++++++++++++-----------
index.html | 23 +++++++++++++----------
pickup.html | 24 +++++++++++++-----------
src/lib/closing-soon.js | 22 +++++++++++-----------
6 files changed, 80 insertions(+), 73 deletions(-)
These are the same six files that the check listed. Stage them by name:
$ git add README.md deals.html departments.html index.html \
pickup.html src/lib/closing-soon.js
Then read the whole staged diff, as you did for the setup. Here’s the navigation in deals.html, shortened to that part, as an example of what to expect:
$ git diff --staged deals.html
diff --git a/deals.html b/deals.html
...
@@ -13,10 +13,10 @@
<a class="logo" href="index.html">Generic Grocery Co.</a>
<nav>
<ul>
- <li><a href="index.html">Home</a></li>
- <li><a href="deals.html" aria-current="page">Weekly deals</a></li>
- <li><a href="departments.html">Departments</a></li>
- <li><a href="pickup.html">Curbside pickup</a></li>
+ <li><a href="index.html">Home</a></li>
+ <li><a href="deals.html" aria-current="page">Weekly deals</a></li>
+ <li><a href="departments.html">Departments</a></li>
+ <li><a href="pickup.html">Curbside pickup</a></li>
</ul>
</nav>
The list items now sit two spaces deeper than the <ul> that holds them, and the text of every line is the same. The pages also get <!doctype html> in lowercase and a / at the end of tags like <meta>, and closing-soon.js gets two-space indentation, double quotes, and semicolons.
The pages look the same in the browser, because the new indentation sits between tags, where the browser doesn’t show it, and Prettier leaves alone the spaces that would show on the page.
Read the rest of the diff the same way. Every removed line should come back as an added line with the same code, changed only in the ways above: layout, quotes, semicolons, and tag details. As in every diff, also check for anything that resembles a key. When it all checks out, run git commit again and write a message that tells a reviewer they can skip this one:
Format the whole project with Prettier
Only layout changes, all made by npm run format, so a review can
skip this commit and read the others.
Save and close the tab to make the commit:
$ git commit
[format-and-lint 227d527] Format the whole project with Prettier
6 files changed, 80 insertions(+), 73 deletions(-)
Now ask Prettier again whether anything is left to do:
$ npx prettier --check .
npm notice run [email protected] npx
npm notice run 'prettier' --check .
Checking formatting...
All matched files use Prettier code style!
And… the whole project is formatted! When a file still needs formatting, --check exits with an error, so a script can tell pass from fail without a person reading the list. A check before merging a pull request relies on that exit status, and npm run check runs the same check too. Its report now has two new lines, shortened here to those:
$ npm run check
...
✔ prettier 3.10.1 is pinned to one exact version.
✔ prettier --check . finds every file formatted.
...
If putting a reformat in its own commit still feels odd to you, then you can talk it through with the mentor. The button below fills in a question:
Format Every Time You Save
Running npm run format before every commit works, but you’ll forget it now and then, and the check only tells you afterward. See for yourself by making a mess on purpose. In src/lib/prices.js, change the unitLabels line so it looks like the previous developer wrote it, and save the file:
const unitLabels = {each: '', lb: '/lb', dozen: '/dozen'}
Then run the check again. Its last line is shortened here:
$ npx prettier --check .
npm notice run [email protected] npx
npm notice run 'prettier' --check .
Checking formatting...
[warn] src/lib/prices.js
[warn] Code style issues found in the above file. Run Prettier with --write…
The check caught it, as it would in a pull request. It’d be nicer if the line never got that far, so have your editor format each file as you save it. In VS Code, that takes the Prettier extension and two settings:
-
Open the Extensions view with Ctrl+Shift+X, or Cmd+Shift+X on macOS, and search for Prettier. Install Prettier - Code formatter, whose publisher is Prettier. Several extensions have similar names, so check the publisher before you install, as you would with an npm package.
-
Open the settings with Ctrl+,, or Cmd+, on macOS, and click the Workspace tab, so the settings apply to this project only.
-
Search for default formatter, and set Editor: Default Formatter to Prettier - Code formatter.
-
Search for format on save, and tick Editor: Format On Save.
Now go back to prices.js, which still has the messy line. Make any small edit, such as adding a space after {, and save.
The line snaps back to const unitLabels = { each: "", lb: "/lb", dozen: "/dozen" };. Have a look at Git:
$ git status
On branch format-and-lint
nothing to commit, working tree clean
The file is back to exactly what you committed. The extension uses the Prettier installation in the project’s node_modules/ and reads its .prettierrc and .prettierignore, so saving a project file in the editor formats it the same way npm run format does.
If VS Code asks whether you trust the folder’s authors, then say yes, because in an untrusted folder the extension falls back to its own built-in Prettier. The workspace settings live in .vscode/settings.json. The project’s .gitignore already keeps that file out of Git, because editor setup is a personal choice.
If the line stayed messy when you saved, then the mentor can help you find out why. Tell it what happened on save and what Editor: Default Formatter and Editor: Format On Save are set to:
Once it’s fixed, save prices.js again and run npx prettier --check . until it says All matched files use Prettier code style! and git status shows a clean working tree. Then the editor and the command line agree.
Keep in mind what Prettier doesn’t do. It only decides how code looks, so a formatted file can still hold a misspelled name or a variable that’s never used. Checking code for potential mistakes is a linter’s job, though even a linter misses some, such as the traps to look for in AI-written code.
Some teams use Biome, a newer tool that both formats and lints code, but Prettier and ESLint are what you’ll meet in most JavaScript projects today.
Check Your Understanding
Try these questions before you open the answers:
-
You run
npx prettier --check ., and it listssrc/home.js. What does that tell you, and what happened to the file? -
Why did you install Prettier with
--save-exact, when the lockfile already records its version? -
A pull request called “Fix the holiday hours” changes every line of
src/lib/hours.js, most of them only in quotes and indentation. What would make it easier to review? -
Prettier rewrote
closing-soon.js, and the check passes. Does that mean the closing-soon notice works now?
-
The file isn’t formatted the way Prettier would lay it out. The file itself didn’t change, because
--checkonly reports.npm run format, or saving the file with format on save, rewrites it. -
A caret range lets
npm updatemove Prettier to a newer release, which can lay out code a little differently. With Prettier pinned, every copy of the project formats the same way until someone updates Prettier on purpose. -
Put the reformat in a commit of its own, with a message that says it only changes layout, and the holiday fix in another commit. Even better, use a formatter on the project from the start so the layout never drifts.
-
No. A formatter only changes layout, never what code does, so the file is tidy but as unfinished as before. Finding mistakes in code is a linter’s job.
Your branch now has two commits, Prettier’s setup and the reformat, and every file passes the check. In the next lesson, you’ll add ESLint to the same branch and find out what the previous developer’s tidy-looking code still gets wrong.