Install
First, install Prettier locally:
- npm
- yarn
- pnpm
- bun
- deno
npm install --save-dev --save-exact prettier@%PRETTIER_VERSION%
yarn add --dev --exact prettier@%PRETTIER_VERSION%
pnpm add --save-dev --save-exact prettier@%PRETTIER_VERSION%
bun add --dev --exact prettier@%PRETTIER_VERSION%
deno install --dev --save-exact prettier@%PRETTIER_VERSION%
Then, create an empty config file to let editors and other tools know you are using Prettier:
node --eval "fs.writeFileSync('.prettierrc','{}\n')"
Next, create a .prettierignore file to let the Prettier CLI and editors know which files to not format. Here’s an example:
node --eval "fs.writeFileSync('.prettierignore','# Ignore artifacts:\nbuild\ncoverage\n')"
Prettier will follow rules specified in .gitignore if it exists in the same directory from which it is run. You can also base your .prettierignore on .eslintignore (if you have one).
If your project isn’t ready to format, say, HTML files yet, add *.html.
Now, format all files with Prettier:
- npm
- yarn
- pnpm
- bun
- deno
npx prettier . --write
What is that npx thing? npx ships with npm and lets you run locally installed tools. We’ll leave off the npx part for brevity throughout the rest of this file!
If you forget to install Prettier first, npx will temporarily download the latest version. That’s not a good idea when using Prettier, because we change how code is formatted in each release! It’s important to have a locked down version of Prettier in your package.json. And it’s faster, too.
yarn exec prettier .