CLI
Use the prettier
command to run Prettier from the command line.
prettier [options] [file/dir/glob ...]
To run your locally installed version of Prettier, prefix the command with npx
or yarn
(if you use Yarn), i.e. npx prettier --help
, or yarn prettier --help
.
To format a file in-place, use --write
. (Note: This overwrites your files!)
In practice, this may look something like:
prettier . --write
This command formats all files supported by Prettier in the current directory and its subdirectories.
It’s recommended to always make sure that prettier --write .
only formats what you want in your project. Use a .prettierignore
file to ignore things that should not be formatted.
A more complicated example:
prettier docs package.json "{app,__{tests,mocks}__}/**/*.js" --write --single-quote --trailing-comma all
Don’t forget the quotes around the globs! The quotes make sure that Prettier CLI expands the globs rather than your shell, which is important for cross-platform usage.
It’s better to use a configuration file for formatting options like --single-quote
and --trailing-comma
instead of passing them as CLI flags. This way the Prettier CLI, editor integrations, and other tooling can all know what options you use.
File patterns
Given a list of paths/patterns, the Prettier CLI first treats every entry in it as a literal path.
-
If the path points to an existing file, Prettier CLI proceeds with that file and doesn’t resolve the path as a glob pattern.
-
If the path points to an existing directory, Prettier CLI recursively finds supported files in that directory. This resolution process is based on file extensions and well-known file names that Prettier and its plugins associate with supported languages.
-
Otherwise, the entry is resolved as a glob pattern using the glob syntax from the
fast-glob
module.
Prettier CLI will ignore files located in node_modules
directory. To opt out from this behavior, use --with-node-modules
flag.
Prettier CLI will not follow symbolic links when expanding arguments.
To escape special characters in globs, one of the two escaping syntaxes can be used: prettier "\[my-dir]/*.js"
or prettier "[[]my-dir]/*.js"
. Both match all JS files in a directory named [my-dir]
, however the latter syntax is preferable as the former doesn’t work on Windows, where backslashes are treated as path separators.
--check
When you want to check if your files are formatted, you can run Prettier with the --check
flag (or -c
).
This will output a human-friendly message and a list of unformatted files, if any.
prettier . --check
Console output if all files are formatted:
Checking formatting...
All matched files use Prettier code style!
Console output if some of the files require re-formatting:
Checking formatting...
[warn] src/fileA.js
[warn] src/fileB.js
[warn] Code style issues found in 2 files. Run Prettier with --write to fix.
The command will return exit code 1
in the second case, which is helpful inside the CI pipelines.
Human-friendly status messages help project contributors react on possible problems.
To minimise the number of times prettier --check
finds unformatted files, you may be interested in configuring a pre-commit hook in your repo.
Applying this practice will minimise the number of times the CI fails because of code formatting problems.
If you need to pipe the list of unformatted files to another command, you can use --list-different
flag instead of --check
.