{"id":20399,"library":"prettier-plugin-packagejson","title":"prettier-plugin-packagejson","description":"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.","status":"active","version":"3.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/matzkoh/prettier-plugin-packagejson","tags":["javascript","package.json","plugin","prettier","typescript"],"install":[{"cmd":"npm install prettier-plugin-packagejson","lang":"bash","label":"npm"},{"cmd":"yarn add prettier-plugin-packagejson","lang":"bash","label":"yarn"},{"cmd":"pnpm add prettier-plugin-packagejson","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency, required for the plugin to work","package":"prettier","optional":false}],"imports":[{"note":"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.","wrong":"require('prettier-plugin-packagejson') in module.exports","symbol":"plugin","correct":"plugins: ['prettier-plugin-packagejson']"},{"note":"Option is camelCase `packageSortOrder` in .prettierrc or Prettier config.","wrong":"{\n  \"sortPackageJson\": [\"name\", \"version\", \"main\"]\n}","symbol":"packageSortOrder","correct":"{\n  \"packageSortOrder\": [\"name\", \"version\", \"main\"]\n}"},{"note":"The plugin does not export any symbols directly; types come from Prettier. Use `import { Plugin } from 'prettier'` if needed.","wrong":"import { Plugin } from 'prettier-plugin-packagejson'","symbol":"type","correct":"import type { Plugin } from 'prettier'"}],"quickstart":{"code":"// Install: npm i -D prettier prettier-plugin-packagejson\n\n// .prettierrc (JSON)\n{\n  \"plugins\": [\"prettier-plugin-packagejson\"],\n  \"packageSortOrder\": [\"name\", \"version\", \"private\", \"description\", \"main\", \"scripts\", \"dependencies\", \"devDependencies\"]\n}\n\n// Example package.json (before)\n{\n  \"scripts\": {\n    \"build\": \"tsc\"\n  },\n  \"name\": \"my-package\",\n  \"version\": \"1.0.0\",\n  \"main\": \"dist/index.js\"\n}\n\n// After running `prettier --write package.json`:\n// Sorted according to packageSortOrder\n{\n  \"name\": \"my-package\",\n  \"version\": \"1.0.0\",\n  \"main\": \"dist/index.js\",\n  \"scripts\": {\n    \"build\": \"tsc\"\n  }\n}","lang":"javascript","description":"Install the plugin, configure with custom sort order, and format package.json."},"warnings":[{"fix":"Upgrade to Prettier v3 or pin to prettier-plugin-packagejson@2.x","message":"v3.0.0 drops support for Prettier v2","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use `packageSortOrder` in config; see v2.5.0 changelog","message":"The option `sortOrder` was renamed to `packageSortOrder`","severity":"deprecated","affected_versions":"<2.5"},{"fix":"Explicitly set `plugins: ['prettier-plugin-packagejson']` in .prettierrc, .prettierrc.js, or prettier.config.js","message":"Plugin must be added to `plugins` array in Prettier config for v3+; auto-loading via `--plugin` CLI flag may not work in all setups","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Ensure package.json is valid JSON; use JSON5 or similar if trailing commas needed, but plugin expects strict JSON","message":"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)","severity":"gotcha","affected_versions":"all"},{"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","message":"The plugin relies on `sort-package-json`; custom sort order may conflict with Prettier's formatting if fields are not recognized","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Install as a dev dependency: `npm i -D prettier-plugin-packagejson` and ensure `plugins: ['prettier-plugin-packagejson']` in config","cause":"Missing or incorrect plugin installation for Prettier v3","error":"Cannot find module 'prettier-plugin-packagejson'"},{"fix":"Run `npm install`; check that the package is in `devDependencies`; use full path: `plugins: ['./node_modules/prettier-plugin-packagejson']` if needed","cause":"Prettier cannot locate the plugin because it's not in node_modules or not specified correctly","error":"Error: Plugin `prettier-plugin-packagejson` not found"},{"fix":"Rename option from `sortOrder` to `packageSortOrder` in Prettier config","cause":"Using deprecated `sortOrder` option name in v2.5+","error":"TypeError: Cannot read properties of undefined (reading 'sortOrder')"},{"fix":"Upgrade Prettier to v3: `npm i -D prettier@latest` or downgrade plugin: `npm i -D prettier-plugin-packagejson@2`","cause":"Prettier v2 is installed, but plugin v3+ only supports Prettier v3","error":"Invalid version: the plugin requires prettier >=3.0.0"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}