跳至主内容区

命令行界面

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

使用 prettier 命令在命令行中运行 Prettier。

prettier [options] [file/dir/glob ...]
备注

要运行本地安装的 Prettier 版本,请在命令前添加 npxyarn execpnpm execbunx 前缀,例如:npx prettier --helpyarn exec prettier --helppnpm exec prettier --helpbunx prettier --help

使用 --write 可原地格式化文件。(注意:此操作会覆盖原文件!)

实际操作示例如下:

prettier . --write

此命令会格式化当前目录及其子目录中所有 Prettier 支持的文件。

建议始终确保 prettier --write . 仅格式化目标内容。使用 .prettierignore 文件排除无需格式化的内容。

更复杂的示例:

prettier docs package.json "{app,__{tests,mocks}__}/**/*.js" --write --single-quote --trailing-comma all
警告

务必在 glob 模式周围添加引号!引号确保由 Prettier CLI(而非 shell)展开 glob 模式,这对跨平台使用至关重要。

备注

推荐使用配置文件(而非 CLI 标志)设置格式化选项(如 --single-quote--trailing-comma)。这样 Prettier CLI、编辑器集成及其他工具都能识别你的配置。

文件匹配模式

给定路径/模式列表时,Prettier CLI 会先将每个条目视为字面路径处理:

  • 若路径指向现有文件,则直接处理该文件(不解析为 glob 模式)

  • 若路径指向现有目录,则递归查找该目录中支持的文件(基于文件扩展名及 Prettier 与其插件关联的已知文件名)

  • 其他情况则使用 fast-glob 模块的 glob 语法解析为 glob 模式

Prettier CLI 默认忽略 node_modules 目录中的文件。使用 --with-node-modules 参数可取消此行为。

Prettier CLI 在展开参数时不会遵循符号链接。

转义 glob 中的特殊字符有两种语法:prettier "\[my-dir]/*.js"prettier "[[]my-dir]/*.js"。两者均可匹配 [my-dir] 目录中的 JS 文件,但推荐后者——前者在 Windows 无效(因反斜杠被视作路径分隔符)。

--check

使用 --check(或 -c)标志可检查文件格式化状态,输出用户友好的提示信息及未格式化文件列表(若存在)。

prettier . --check

全部文件已格式化时的控制台输出:

Checking formatting...
All matched files use Prettier code style!

部分文件需重新格式化时的控制台输出:

Checking formatting...
[warn] src/fileA.js
[warn] src/fileB.js
[warn] Code style issues found in 2 files. Run Prettier with --write to fix.

第二种情况命令会返回退出码 1(便于 CI 管道处理)。友好的状态提示有助于开发者快速定位问题。建议配置预提交钩子来减少 prettier --check 发现未格式化文件的次数,从而降低 CI 因代码格式化问题失败的频率。

若需将未格式化文件列表传递给其他命令,请使用 --list-different 标志替代 --check

退出码

CodeInformation
0Everything formatted properly
1Something wasn’t formatted properly
2Something’s wrong with Prettier

--debug-check

若担心 Prettier 会更改代码的正确性,请在命令中添加 --debug-check 参数。这将导致 Prettier 在检测到代码正确性可能发生变化时输出错误信息。请注意 --write 不能与 --debug-check 同时使用。

--find-config-path--config

如果重复使用 prettier 格式化单个文件,Prettier 在查找配置文件时会产生少量性能开销。为避免此开销,可让 Prettier 一次性找到配置文件并后续复用。

$ prettier --find-config-path path/to/file.js
path/to/.prettierrc

这将提供配置文件的路径,您可将其传递给 --config

prettier path/to/file.js --write --config path/to/.prettierrc

若配置文件位于 Prettier 无法自动发现的位置(例如 config/ 目录),也可使用 --config 参数。

若没有配置文件或希望忽略现有配置,可改用 --no-config 参数。

--ignore-path

指向包含忽略文件模式的文件路径。默认情况下,Prettier 会查找 ./.gitignore./.prettierignore
支持多个值。

--list-different

另一个实用参数是 --list-different(或 -l),它会输出与 Prettier 格式化结果不同的文件名。若存在差异,脚本将以错误状态退出,这在 CI 场景中非常有用。

prettier . --single-quote --list-different

也可使用 --check 参数,其工作方式与 --list-different 相同,但会额外向 stdout 输出人性化的摘要信息。

--no-config

不查找配置文件,将使用默认设置。

--config-precedence

定义配置文件应如何与 CLI 选项协同生效。

cli-override(默认)

CLI 选项优先级高于配置文件

file-override

配置文件优先级高于 CLI 选项

prefer-file

若找到配置文件,则使用该配置并忽略其他 CLI 选项;若未找到配置文件,则按常规方式处理 CLI 选项。

此选项支持编辑器集成场景:用户可定义默认配置,同时遵循项目特定配置。

--no-editorconfig

解析配置时不考虑 .editorconfig 文件。详见 prettier.resolveConfig 文档

--with-node-modules

Prettier CLI 默认忽略 node_modules 目录中的文件。使用 --with-node-modules 参数可取消此行为。

--write

此参数将原地重写所有处理过的文件,工作流类似于 eslint --fix。也可使用 -w 简写形式。

--log-level

更改 CLI 的日志输出级别。有效选项包括:

  • error

  • warn

  • log(默认)

  • debug

  • silent

--stdin-filepath

指定文件路径时,Prettier CLI 会将其视为标准输入(stdin)处理。例如:

abc.css

.name {
display: none;
}

shell

$ cat abc.css | prettier --stdin-filepath abc.css
.name {
display: none;
}

--ignore-unknown

使用 --ignore-unknown(或 -u)参数时,prettier 会忽略模式匹配到的未知类型文件。

prettier "**/*" --write --ignore-unknown

--no-error-on-unmatched-pattern

当模式未匹配到文件时禁止报错。

--cache

启用此选项后,以下值将作为缓存密钥,仅当其中任一值发生变更时才会重新格式化文件:

  • Prettier 版本

  • 配置选项

  • Node.js 版本

  • (当 --cache-strategymetadata 时) 文件元数据(如时间戳)

  • (当 --cache-strategycontent 时) 文件内容

prettier . --write --cache

未使用 --cache 参数运行 Prettier 将自动清除缓存。

缓存文件默认存储在 ./node_modules/.cache/prettier/.prettier-cache,也可通过 rm ./node_modules/.cache/prettier/.prettier-cache 手动删除。

警告

插件版本及实现逻辑不作为缓存校验依据。建议更新插件后主动清除缓存。

--cache-location

指定 --cache 使用的缓存文件路径。未显式设置 --cache-location 时,默认存储在 ./node_modules/.cache/prettier/.prettier-cache

若参数为文件路径,则直接使用该文件作为缓存。

prettier . --write --cache --cache-location=path/to/cache-file

--cache-strategy

缓存检测文件变更的策略,可选 metadata(元数据)或 content(内容)。

通常 metadata 策略速度更快。但 content 策略适用于文件时间戳变更而内容未修改的场景(例如执行 git clone 操作时,Git 不记录文件修改时间)。

未指定策略时默认采用 content

prettier . --write --cache --cache-strategy metadata