Prettier Plugin Package

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

An opinionated package.json formatter plugin for Prettier, enforcing a consistent key ordering style popularized by Sindre Sorhus. v2.0.0 requires Node >=20.19.0 and Prettier ^3.0.0. It sorts top-level keys, and alphabetizes scripts, files, and engines. Differentiates from other package.json formatters by its strict opinionated ordering and automatic key classification. Released under MPL-2.0, maintained by shellscape, with monthly releases.

error Cannot find module 'prettier-plugin-package'
cause Plugin not installed or not in node_modules when running Prettier.
fix
Run npm install --save-dev prettier-plugin-package. Ensure prettier is also installed.
error Error: No parser and no file path given, couldn't infer a parser.
cause Prettier invoked without a file; plugin only formats package.json, not other files.
fix
Run Prettier explicitly on package.json: npx prettier --write package.json
error prettier-plugin-package: Node.js version 20.19.0 is required. Current version: ...
cause Node.js version below 20.19.0 with plugin v2.
fix
Upgrade Node.js to >=20.19.0 or downgrade plugin to v1.4.0 using npm install prettier-plugin-package@1.4.0 --save-dev
error prettier-plugin-package: prettier version ^3.0.0 is required.
cause Prettier v2 is installed but plugin v2 requires v3.
fix
Install prettier v3: npm install prettier@^3.0.0 or downgrade plugin to v1.4.0 with npm install prettier-plugin-package@1.4.0 --save-dev
breaking Node.js >=20.19.0 required (v2 release)
fix Upgrade Node.js to v20.19.0 or later, or use prettier-plugin-package@1.4.0 for Node 14+ and Prettier v2.
breaking Prettier v3 only (v2 release). v1.x and v2.x not supported.
fix Use prettier-plugin-package@1.4.0 for Prettier v2, or @0.3.1 for Prettier v1.
breaking Plugin no longer uses preprocess; switched to parser in v0.3.0. Behavior may differ.
fix Ensure you use Prettier v2+ and run via standard prettier --write. No migration steps required.
gotcha Unknown keys are sorted alphabetically after known keys. This may reorder unsorted custom keys unexpectedly.
fix Review resulting package.json order; if custom keys appear in unexpected order, consider renaming keys to match alphabetical order or accept the opinionated sort.
gotcha The files array gets reordered: alphabetical, with negations kept together, and README.md / LICENSE appended if present.
fix Verify that your files array after formatting includes all intended entries; the plugin ensures README.md and LICENSE are last if present.
npm install prettier-plugin-package
yarn add prettier-plugin-package
pnpm add prettier-plugin-package

Demonstrates automatic sorting of keys and scripts in package.json after running Prettier with the plugin.

// Install: npm install --save-dev prettier prettier-plugin-package

// package.json (initial state)
{
  "devDependencies": { "prettier": "^3.0.0" },
  "scripts": { "build": "echo build", "test": "echo test", "lint": "echo lint" },
  "description": "My package",
  "name": "my-pkg",
  "version": "1.0.0"
}

// After running: npx prettier --write package.json
// The keys will be reordered per the plugin's sort order:
{
  "name": "my-pkg",
  "version": "1.0.0",
  "description": "My package",
  "scripts": {
    "build": "echo build",
    "lint": "echo lint",
    "test": "echo test"
  },
  "devDependencies": {
    "prettier": "^3.0.0"
  }
}