Sub topic 8
Installing and Running Prettier
This chapter assumes you can run Node.js and npm from the command line, edit package.json, and read modern JavaScript. Prettier is an opinionated code formatter. It parses your code and prints it again in a consistent style. It does not look for bugs. That job belongs to ESLint.
Installing Prettier locally
Install Prettier as a development dependency of the project. A global install would let each developer run a different version.
npm install --save-dev --save-exact prettierThe --save-exact flag pins one exact version, such as 3.3.3, instead of a range like ^3.3.3. Minor Prettier releases can change formatting output. If developers or CI resolve different versions, the same file can be formatted two different ways, and every commit picks up unrelated diffs.
Next, create a config file. An empty object is enough, and it tells editors and tools that the project uses Prettier:
node --eval "require('fs').writeFileSync('.prettierrc', '{}\n')"The next chapter covers what to put in this file.
Running Prettier
Prettier has two main modes. --write rewrites files in place. --check only reports which files are unformatted.
npx prettier . --write
npx prettier . --check
npx prettier src/app.js --writeThe . argument means "every supported file under this directory." Prettier formats JavaScript, TypeScript, JSON, CSS, Markdown, YAML and more. Since version 3, it skips files listed in .gitignore and node_modules by default.
Here is a file before formatting:
const greet=(name)=>{return "Hello, "+name}
console.log(greet( 'Ada' ))With default options, npx prettier greet.js --write produces this:
const greet = (name) => {
return "Hello, " + name;
};
console.log(greet("Ada"));Prettier added spaces and semicolons, switched to double quotes, and broke the function body onto its own lines. It did not change what the code does.
Exit codes and common mistakes
--check exits with code 1 when any file needs formatting, and that makes it a good fit for CI. --write exits with 0 even when it changed files, so it cannot act as a gate.
| Mistake | Symptom | Fix |
|---|---|---|
| Unpinned version | Formatting differs between machines | Install with `--save-exact` |
| Running `--write` in CI | CI passes on unformatted code | Use `--check` in CI |
| Formatting generated files | Huge diffs in `dist/` | Add the paths to `.prettierignore` |
| Global Prettier in the editor | Editor output differs from the CLI | Point the editor at the local install |
Exercise
A teammate adds npx prettier . --write as a CI step and reports that CI never fails on badly formatted pull requests. Why, and what command should CI run instead?
Show answer
--write fixes files inside the CI runner and exits with code 0, so the job always passes and the fixes are thrown away. CI should run npx prettier . --check, which exits with code 1 and lists the unformatted files.
Exercise
Why install Prettier with --save-exact instead of the default caret range?
Show answer
Minor and patch releases of Prettier can change its output. Pinning one exact version means every developer, editor, and CI run formats code the same way.