API Reference

Prettier

Opinionated code formatter exposing a Node API to format, check, and resolve configuration for many languages.

Official Documentation ↗
CompatibilityThis documentation covers version v3. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev prettier
pnpmpnpm add -D prettier

Methods

Overview

Prettier is an opinionated formatter with a Node API to format and verify source in many languages. It deliberately avoids configurable stylistic choices. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • prettier.format — Formats the given source string and resolves with the formatted result.
  • prettier.check — Returns true when the source already matches Prettier output, useful for CI gates.
  • prettier.resolveConfig — Finds and resolves the configuration that applies to a given file path.

Example

const prettier = require('prettier');

const out = await prettier.format('const x={a:1};', {
  parser: 'babel',
  semi: true,
});
console.log(out);

Notes

The API does not infer parsers from code, so always pass parser or filepath. Plugins can extend formatting but also change output, so pin versions.

Verification

Format a known unformatted snippet and confirm prettier.check returns false before and true after applying the output.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://prettier.io/docs/en/api.html · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:48.942Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

Why do I get a parser error from the API?
The API requires an explicit parser or filepath; pass parser, or filepath so Prettier infers the parser from the extension.
How do I ignore specific files?
Add patterns to .prettierignore; the CLI and the API both honor it via getFileInfo and getIgnoreFiles.
Does Prettier sort imports?
No. Use @ianvs/prettier-plugin-sort-imports or a similar plugin, since Prettier only handles formatting by default.