{"id":20333,"library":"prettier-package-json","title":"Prettier Package JSON","description":"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.","status":"active","version":"2.8.0","language":"javascript","source_language":"en","source_url":"https://github.com/cameronhunter/prettier-package-json","tags":["javascript","cleanup","formatter","json","keys","object","package","package.json","prettier"],"install":[{"cmd":"npm install prettier-package-json","lang":"bash","label":"npm"},{"cmd":"yarn add prettier-package-json","lang":"bash","label":"yarn"},{"cmd":"pnpm add prettier-package-json","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Dependency for sorting object keys deterministically","package":"sort-object-keys","optional":false}],"imports":[{"note":"ESM import for the primary formatting function. CJS require is also supported but named export must be destructured.","wrong":"const format = require('prettier-package-json')","symbol":"format","correct":"import { format } from 'prettier-package-json'"},{"note":"check is a named export, not default. Use check() to verify formatting without writing.","wrong":"import check from 'prettier-package-json'","symbol":"check","correct":"import { check } from 'prettier-package-json'"},{"note":"Predefined options object for customization. Available since v2.0.","wrong":null,"symbol":"defaultOptions","correct":"import { defaultOptions } from 'prettier-package-json'"}],"quickstart":{"code":"const { format, check } = require('prettier-package-json');\nconst fs = require('fs');\n\nconst pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'));\nconst formatted = format(pkg, {\n  indent: '  ',\n  expandUsers: false,\n  useTabs: false\n});\nfs.writeFileSync('./package.json', JSON.stringify(formatted, null, 2) + '\\n');\nconsole.log('Formatted package.json');\n\n// Check if already formatted\nconst isFormatted = check(pkg, { expandUsers: true });\nconsole.log('Is formatted:', isFormatted);","lang":"javascript","description":"Reads package.json, formats with indent and expandUsers options, writes back, and demonstrates the check function."},"warnings":[{"fix":"Treat the returned value as the formatted output; do not rely on the original object being changed.","message":"In v1.x, format() mutated the input object. In v2+, it returns a new object and the input is not modified.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Remove those options; sorting is always applied. Use noSort option to disable sorting entirely.","message":"The options `sortScripts` and `sortFiles` have been deprecated in favor of unified sorting behavior.","severity":"deprecated","affected_versions":">=2.5.0"},{"fix":"Use version control or manually back up package.json before running with --write.","message":"The CLI uses `--write` to overwrite the file, but it does not create backups. Always commit before running.","severity":"gotcha","affected_versions":"all"},{"fix":"Do not include npm-implied files explicitly; they will be removed on format.","message":"The `files` field is automatically filtered: it excludes files that npm always includes (e.g., package.json, README, CHANGELOG).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set `expandUsers: true` to keep object format, or ensure objects only have name/email/url.","message":"When using `expandUsers: false` (default), author, contributors, and maintainers are converted to string format. This may lose information if objects contain extra keys beyond name/email/url.","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":"Run `npm install prettier-package-json --save-dev` or `yarn add prettier-package-json --dev`.","cause":"Package not installed or misspelled in require/import.","error":"Error: Cannot find module 'prettier-package-json'"},{"fix":"Use `import { format } from 'prettier-package-json'` or `const { format } = require('prettier-package-json')`.","cause":"Incorrect import: using default import instead of named export.","error":"TypeError: format is not a function"},{"fix":"Parse the JSON string first: `const pkg = JSON.parse(rawString);` then call `format(pkg)`.","cause":"Passed raw JSON string instead of parsed object to format().","error":"Error: Expected a valid JSON object, got string"},{"fix":"Remove sortScripts and sortFiles from options. Sorting is always applied; use noSort: true to disable.","cause":"Using deprecated options in v2.5+.","error":"Error: Options object should not contain property 'sortScripts'"},{"fix":"Ensure the argument to format() is a valid parsed JSON object, not null or undefined.","cause":"Passed null or undefined as the package object.","error":"TypeError: Cannot read property 'sort' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}