prettier-format

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

Auto-loads Prettier config and formats code with a single async function. Current stable version is 4.0.0, released with breaking changes removing the default 'babel' parser and fixing the `filePath` option casing. Released on npm, maintained by fisker. Key differentiator: eliminates need to manually call `prettier.resolveConfig` and `prettier.format` separately; all configuration loading is done internally based on `filepath`.

error Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported.
cause Using CommonJS require() on an ESM-only package.
fix
Switch to import statement or use dynamic import: const format = (await import('prettier-format')).default
error TypeError: format is not a function
cause Default import not used; attempting to use named import or destructuring incorrectly.
fix
Use import format from 'prettier-format' (default import).
error Error: No parser and no filepath given, cannot infer parser.
cause Calling format() without filepath or parser option (breaking change in v4).
fix
Provide a 'filepath' option or an explicit 'parser' option like 'babel'.
error Warning: 'filePath' option is deprecated. Use 'filepath' instead.
cause Using camelCase 'filePath' instead of lowercase 'filepath'.
fix
Replace 'filePath' with 'filepath' in options.
breaking Default parser 'babel' removed in v4.0.0; must pass 'parser' or 'filepath' option.
fix Always provide 'filepath' option (or explicit 'parser') when calling format().
breaking Option 'filePath' (camelCase) no longer works; use 'filepath' (lowercase) instead.
fix Replace 'filePath' with 'filepath' in options object.
deprecated Support for Prettier <3 dropped; requires Prettier 3.x.
fix Upgrade Prettier to version 3 or later.
gotcha Module is ESM-only since v3; cannot be require()'d.
fix Use import or dynamic import instead of require().
npm install prettier-format
yarn add prettier-format
pnpm add prettier-format

Demonstrates using the default export to format source code with automatic config loading based on filepath.

import format from 'prettier-format';

// Format with auto-loaded config from file path
const formatted = await format('const x = 1', {
  filepath: '/path/to/file.js',
  semi: false,
});
console.log(formatted);
// 'const x = 1\n'