Registry / testing / prettier-plugin-packagejson

prettier-plugin-packagejson

JSON →
library3.0.2jsnpmunverified

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.

npm install prettier-plugin-packagejson
INSTALL
IMPORT
SIG · PRETTIER-PLUGIN-PA
P
prettier-plugin-packagejson
testingjavascriptv3.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

plugin
plugins: ['prettier-plugin-packagejson']
require('prettier-plugin-packagejson') in module.exports
Add the plugin string to Prettier's plugins array. No explicit import required; Prettier resolves it automatically. For ESM config, use `plugins: ['./node_modules/prettier-plugin-packagejson']` or let Prettier resolve.
packageSortOrder
{ "packageSortOrder": ["name", "version", "main"] }
{ "sortPackageJson": ["name", "version", "main"] }
Option is camelCase `packageSortOrder` in .prettierrc or Prettier config.
type
import type { Plugin } from 'prettier'
import { Plugin } from 'prettier-plugin-packagejson'
The plugin does not export any symbols directly; types come from Prettier. Use `import { Plugin } from 'prettier'` if needed.

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" } }
Debug
Known issues
breakingv3.0.0 drops support for Prettier v2
fix
Upgrade to Prettier v3 or pin to prettier-plugin-packagejson@2.x
affects: >=3.0.0
deprecatedThe option `sortOrder` was renamed to `packageSortOrder`
fix
Use `packageSortOrder` in config; see v2.5.0 changelog
affects: <2.5
gotchaPlugin 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
affects: >=3.0.0
gotchaSorting 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
affects: all
gotchaThe 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
affects: all
Errors
Common errors & fixes
Cannot find module 'prettier-plugin-packagejson'
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: Plugin `prettier-plugin-packagejson` not found
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
TypeError: Cannot read properties of undefined (reading 'sortOrder')
Using deprecated `sortOrder` option name in v2.5+
fix
Rename option from `sortOrder` to `packageSortOrder` in Prettier config
Invalid version: the plugin requires prettier >=3.0.0
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`
Upgrade
Version history
3.0.2latest on npm
Audit
Dependencies
prettierrequiredpeer dependency, required for the plugin to work
Agent activity
6 hits · last 30 days
node
6
Resources
prettier-plugin-packagejson — npm install prettier-plugin-packagejson · libregistry