unified-prettier

raw JSON →
2.0.1 verified Sat Apr 25 auth: no javascript

A unified plugin that formats output using Prettier. Current stable version is 2.0.1, released in 2024. It integrates with remark-cli, rehype-cli, or any unified-engine based tool to automatically run Prettier on processed files. Key differentiators: it uses Prettier's configuration file by default, supports Prettier plugins, and is fully typed with TypeScript. Unlike running Prettier separately, it ensures formatting happens as part of the unified pipeline, reducing overhead. Requires Node.js >=16, Prettier ^3.0.0, and unified ^11.

error SyntaxError: Unexpected token 'export'
cause Using require() on an ESM-only package (v2.0.0+).
fix
Switch to import syntax, or set type: module in package.json, or downgrade to v1.x.
error Error: Cannot find module 'prettier'
cause Peer dependency prettier is not installed.
fix
Run npm install prettier@^3.0.0.
error TypeError: Cannot read properties of undefined (reading 'format')
cause No file path set in vfile, and formatting is skipped. Attempting to access formatted output that doesn't exist.
fix
Ensure vfile.path is set, or expect that the output is unchanged without formatting.
error Error: You have used a named export instead of default export
cause Using import { unifiedPrettier } from 'unified-prettier' instead of default import.
fix
Use import unifiedPrettier from 'unified-prettier'.
breaking v2.0.0 requires unified 11, breaking compatibility with unified 10.
fix Upgrade unified to version 11 or stay on unified-prettier 1.x.
breaking v2.0.0 drops support for Node.js <16.
fix Use Node.js 16 or later.
breaking v2.0.0 switches to ESM-only. CommonJS require() will not work.
fix Use dynamic import() or switch to ESM.
deprecated v1.x used @prettier/sync internally; v2 uses Prettier's synchronous API.
fix No action needed, but note that asynchronous Prettier API is not used.
gotcha The plugin formats only if a file path is provided in the vfile; otherwise it is ignored since v2.0.1.
fix Ensure the vfile has a path (or set it via file.path).
npm install unified-prettier
yarn add unified-prettier
pnpm add unified-prettier

Shows basic usage of unified-prettier with remark to format a markdown file, including passing Prettier options.

import { remark } from 'remark'
import { read } from 'to-vfile'
import unifiedPrettier from 'unified-prettier'

const processor = remark().use(unifiedPrettier, { semi: false })
const file = await read('README.md')
const { value } = await processor.process(file)
console.log(value)