Prettier-package-json is a focused tool for formatting package.json files with consistent key ordering, script sorting, and author/contributor normalization. Version 2.8.0 is the current stable release, with active maintenance and monthly releases. It differs from generic JSON formatters by applying npm-specific rules: opinionated key order, smart script sorting (keeping pre/post scripts adjacent), filtering automatically included files from the `files` array, and expanding/contracting user objects. It provides both a CLI and programmatic API, supports pre-commit hooks via lint-staged, and is configurable via options like --expand-users. Designed for Node.js environments, it outputs valid JSON with deterministic formatting.
npm install prettier-package-jsonVerified import paths — ran on the pinned version, not inferred.
Reads package.json, formats with indent and expandUsers options, writes back, and demonstrates the check function.
Treat the returned value as the formatted output; do not rely on the original object being changed.
Remove those options; sorting is always applied. Use noSort option to disable sorting entirely.
Use version control or manually back up package.json before running with --write.
Do not include npm-implied files explicitly; they will be removed on format.
Set `expandUsers: true` to keep object format, or ensure objects only have name/email/url.
Run `npm install prettier-package-json --save-dev` or `yarn add prettier-package-json --dev`.
Use `import { format } from 'prettier-package-json'` or `const { format } = require('prettier-package-json')`.Parse the JSON string first: `const pkg = JSON.parse(rawString);` then call `format(pkg)`.
Remove sortScripts and sortFiles from options. Sorting is always applied; use noSort: true to disable.
Ensure the argument to format() is a valid parsed JSON object, not null or undefined.