Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
This document provides instructions on how to install and run Prettier in your project.
The recommended approach is to install Prettier locally as a development dependency.
npm install --save-dev --save-exact prettier
yarn add --dev --exact prettier
pnpm add --save-dev --save-exact prettier
bun add --dev --exact prettier
This repository includes a default configuration file, .prettierrc
, so you don’t need to create one from scratch. It lets editors and other tools know that you are using Prettier.
It is also useful to create a .prettierignore
file to specify which files should not be formatted by Prettier.
echo "# Ignore artifacts:\nbuild\ncoverage" > .prettierignore
Once Prettier is installed, you can run it from the command line to format your files.
To format all files in your project, run the following command:
npx prettier . --write
You can also format specific files or directories:
# Format all files in the 'app' directory
npx prettier app --write
# Format a single file
npx prettier app/components/Button.js --write
# Format all test files in a directory using a glob pattern
npx prettier "app/**/*.test.js" --write
In a Continuous Integration (CI) environment, you can use the --check
flag to verify that all files are formatted correctly without actually modifying them. This is useful for enforcing a consistent code style.
npx prettier . --check
For more information, please refer to the official Prettier documentation.