prettier-plugin-pkg
raw JSON → 0.22.1 verified Sat Apr 25 auth: no javascript
An opinionated package.json formatter plugin for Prettier (v0.22.1, released 2024-03-14). It sorts top-level keys in a custom order, alphabetizes scripts (with pre/post script grouping), engines, files, and dependencies. Provides presets 'npm' and 'npm-plus' via `packageSortOrderPreset`. Requires Prettier ^3.0.3. Unlike prettier-plugin-packagejson, it offers more opinionated sorting and supports custom sort order via `packageSortOrder`.
Common errors
error Error: Cannot find module 'prettier-plugin-pkg' ↓
cause Plugin not installed or not in node_modules; or Prettier is not installed.
fix
Run
npm install --save-dev prettier prettier-plugin-pkg and ensure both are in devDependencies. error Error: prettier-plugin-pkg: Your configuration's plugin entry 'prettier-plugin-pkg' is not working ↓
cause Prettier version mismatch (requires Prettier ^3.0.3) or plugin not loaded.
fix
Upgrade Prettier to 3.x. Check that plugin is installed and listed in plugins array.
error Warning: Ignoring unknown option 'packageSortOrderPreset' ↓
cause Using old plugin version (<0.21.0) that does not support packageSortOrderPreset.
fix
Update prettier-plugin-pkg to v0.21.0+ with
npm install prettier-plugin-pkg@latest. error TypeError: Cannot read properties of undefined (reading 'sort') ↓
cause Attempting to use require('prettier-plugin-pkg') directly; the module does not export a function for sorting.
fix
Remove direct require; use only via Prettier configuration.
Warnings
gotcha Plugin only works with Prettier v3. Using with Prettier v2 will throw an error or silently fail. ↓
fix Upgrade Prettier to ^3.0.3 (or higher).
breaking v0.22.0 changed handling of pre/post scripts: now sorts them together with base script, except for prepare, preprepare, postpare, postprepare. ↓
fix Check script ordering after upgrade; may need to adjust script names if custom ordering changed.
deprecated CJS require() import pattern is deprecated; package uses ESM exports only (since v0.21.2). ↓
fix Use ESM import syntax or rely on Prettier plugin resolution; avoid require('prettier-plugin-pkg').
gotcha The plugin sorts unknown keys alphabetically and appends them. If you have a custom top-level key not in the known list, it may end up in unexpected position. ↓
fix Use packageSortOrder to explicitly place custom keys or set packageIgnoreSort for specific keys.
Install
npm install prettier-plugin-pkg yarn add prettier-plugin-pkg pnpm add prettier-plugin-pkg Imports
- prettier-plugin-pkg wrong
const plugin = require('prettier-plugin-pkg')correctplugins: ['prettier-plugin-pkg'] in .prettierrc - packageSortOrder wrong
// incorrect: using as import import { packageSortOrder } from 'prettier-plugin-pkg'correct// .prettierrc { "packageSortOrder": ["name", "version", "description"] } - packageIgnoreSort
// .prettierrc { "packageIgnoreSort": true }
Quickstart
// 1. Install
// npm install --save-dev prettier prettier-plugin-pkg
// 2. Create .prettierrc.json
{
"plugins": ["prettier-plugin-pkg"],
"packageSortOrderPreset": "npm"
}
// 3. Format package.json
// npx prettier --write package.json
// Example package.json input:
{
"description": "A sample package",
"name": "my-package"
}
// Output:
{
"name": "my-package",
"description": "A sample package"
}