prettier-plugin-sort-package-json

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

Prettier plugin that automatically sorts the keys in package.json files. Current stable version is 1.1.0, released in April 2025. This plugin is a zero-dependency, opinionated tool that works out of the box with no configuration. It supports all keys defined in the npm v11 package.json documentation and also provides a custom parser for sorting package.json-like files. Unlike alternatives (e.g., sort-package-json standalone CLI), it integrates directly into Prettier's workflow, enabling auto-formatting on save and consistent sorting across teams without extra scripts.

error Cannot find module 'prettier-plugin-sort-package-json'
cause Plugin not installed or not in node_modules
fix
npm install -D prettier-plugin-sort-package-json
error Error: Invalid parser 'sort-package-json'. Did you mean 'json'?
cause Using parser 'sort-package-json' without adding the plugin to plugins list
fix
Add "plugins": ["prettier-plugin-sort-package-json"] to Prettier config
error Unsupported option "plugins" in Prettier config (prettier-plugin-sort-package-json).
cause Prettier version is too old (< 3.0.0) and doesn't support plugins
fix
Update Prettier to v3 or higher: npm install -D prettier@latest
breaking Version 0.2.0 reordered 'license' and 'funding' keys — existing files may have different order after update.
fix No action needed — order is now consistent with npm docs. Run Prettier to update files.
deprecated Versions below 1.0.0 are deprecated; v1.0.0 added full support for all npm v11 keys.
fix Upgrade to latest: npm install -D prettier-plugin-sort-package-json@latest
gotcha The plugin only sorts package.json by default; for other .json files you must specify a custom parser in overrides.
fix Add overrides to .prettierrc: { "files": ["pkg.json"], "options": { "parser": "sort-package-json" } }
gotcha The plugin requires Prettier v3 or higher (peer dependency).
fix Ensure Prettier version is ^3.0.0: npm install -D prettier@3
gotcha If you use Prettier's CLI --plugin flag, ensure the plugin is installed and the flag points to the correct path.
fix Use --plugin=prettier-plugin-sort-package-json or just add to config.
npm install prettier-plugin-sort-package-json
yarn add prettier-plugin-sort-package-json
pnpm add prettier-plugin-sort-package-json

Installs the plugin, adds it to Prettier config, and formats package.json with automatic sorting.

// 1. Install
npm install -D prettier prettier-plugin-sort-package-json

// 2. Create .prettierrc
{
  "plugins": ["prettier-plugin-sort-package-json"]
}

// 3. Run Prettier
npx prettier --write package.json

// package.json will now have sorted keys:
{
  "name": "my-package",
  "version": "1.0.0",
  "description": "...",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "test": "jest"
  },
  "dependencies": {},
  "devDependencies": {}
}