prettier-plugin-packagejson

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

A Prettier plugin that automatically sorts the keys of package.json files using sort-package-json. The current stable version is 3.0.2, released March 2026. It releases frequently (multiple versions per month) and only supports Prettier v3+ (dropped v2 support in v3.0.0). Key differentiators: provides a customizable sort order via the `packageSortOrder` option, ships TypeScript type declarations, and integrates seamlessly with Prettier's plugin system.

error Cannot find module 'prettier-plugin-packagejson'
cause Missing or incorrect plugin installation for Prettier v3
fix
Install as a dev dependency: npm i -D prettier-plugin-packagejson and ensure plugins: ['prettier-plugin-packagejson'] in config
error Error: Plugin `prettier-plugin-packagejson` not found
cause Prettier cannot locate the plugin because it's not in node_modules or not specified correctly
fix
Run npm install; check that the package is in devDependencies; use full path: plugins: ['./node_modules/prettier-plugin-packagejson'] if needed
error TypeError: Cannot read properties of undefined (reading 'sortOrder')
cause Using deprecated `sortOrder` option name in v2.5+
fix
Rename option from sortOrder to packageSortOrder in Prettier config
error Invalid version: the plugin requires prettier >=3.0.0
cause Prettier v2 is installed, but plugin v3+ only supports Prettier v3
fix
Upgrade Prettier to v3: npm i -D prettier@latest or downgrade plugin: npm i -D prettier-plugin-packagejson@2
breaking v3.0.0 drops support for Prettier v2
fix Upgrade to Prettier v3 or pin to prettier-plugin-packagejson@2.x
deprecated The option `sortOrder` was renamed to `packageSortOrder`
fix Use `packageSortOrder` in config; see v2.5.0 changelog
gotcha Plugin must be added to `plugins` array in Prettier config for v3+; auto-loading via `--plugin` CLI flag may not work in all setups
fix Explicitly set `plugins: ['prettier-plugin-packagejson']` in .prettierrc, .prettierrc.js, or prettier.config.js
gotcha Sorting may rearrange fields to top of file, affecting comments and trailing commas if not properly handled in JSON (JSON does not allow trailing commas)
fix Ensure package.json is valid JSON; use JSON5 or similar if trailing commas needed, but plugin expects strict JSON
gotcha The plugin relies on `sort-package-json`; custom sort order may conflict with Prettier's formatting if fields are not recognized
fix Test custom `packageSortOrder` on a sample file; if a field is not in the array, it will be placed after sorted ones in an undefined order
npm install prettier-plugin-packagejson
yarn add prettier-plugin-packagejson
pnpm add prettier-plugin-packagejson

Install the plugin, configure with custom sort order, and format package.json.

// Install: npm i -D prettier prettier-plugin-packagejson

// .prettierrc (JSON)
{
  "plugins": ["prettier-plugin-packagejson"],
  "packageSortOrder": ["name", "version", "private", "description", "main", "scripts", "dependencies", "devDependencies"]
}

// Example package.json (before)
{
  "scripts": {
    "build": "tsc"
  },
  "name": "my-package",
  "version": "1.0.0",
  "main": "dist/index.js"
}

// After running `prettier --write package.json`:
// Sorted according to packageSortOrder
{
  "name": "my-package",
  "version": "1.0.0",
  "main": "dist/index.js",
  "scripts": {
    "build": "tsc"
  }
}