{"id":20401,"library":"prettier-plugin-pkg","title":"prettier-plugin-pkg","description":"An opinionated package.json formatter plugin for Prettier (v0.22.1, released 2024-03-14). It sorts top-level keys in a custom order, alphabetizes scripts (with pre/post script grouping), engines, files, and dependencies. Provides presets 'npm' and 'npm-plus' via `packageSortOrderPreset`. Requires Prettier ^3.0.3. Unlike prettier-plugin-packagejson, it offers more opinionated sorting and supports custom sort order via `packageSortOrder`.","status":"active","version":"0.22.1","language":"javascript","source_language":"en","source_url":"https://github.com/un-ts/prettier","tags":["javascript","package","package.json","pkg","plugin","prettier","prettier-plugin","typescript"],"install":[{"cmd":"npm install prettier-plugin-pkg","lang":"bash","label":"npm"},{"cmd":"yarn add prettier-plugin-pkg","lang":"bash","label":"yarn"},{"cmd":"pnpm add prettier-plugin-pkg","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency: plugin requires Prettier 3.x to load and format","package":"prettier","optional":false}],"imports":[{"note":"This is a Prettier plugin, not a Node module to import directly. Use it by adding to prettier config plugins array.","wrong":"const plugin = require('prettier-plugin-pkg')","symbol":"prettier-plugin-pkg","correct":"plugins: ['prettier-plugin-pkg'] in .prettierrc"},{"note":"Option available since v0.21.0. Set in Prettier config, not imported.","wrong":"// incorrect: using as import\nimport { packageSortOrder } from 'prettier-plugin-pkg'","symbol":"packageSortOrder","correct":"// .prettierrc\n{\n  \"packageSortOrder\": [\"name\", \"version\", \"description\"]\n}"},{"note":"Option added in v0.21.0. Set in Prettier config to skip sorting specific keys.","wrong":null,"symbol":"packageIgnoreSort","correct":"// .prettierrc\n{\n  \"packageIgnoreSort\": true\n}"}],"quickstart":{"code":"// 1. Install\n// npm install --save-dev prettier prettier-plugin-pkg\n\n// 2. Create .prettierrc.json\n{\n  \"plugins\": [\"prettier-plugin-pkg\"],\n  \"packageSortOrderPreset\": \"npm\"\n}\n\n// 3. Format package.json\n// npx prettier --write package.json\n\n// Example package.json input:\n{\n  \"description\": \"A sample package\",\n  \"name\": \"my-package\"\n}\n// Output:\n{\n  \"name\": \"my-package\",\n  \"description\": \"A sample package\"\n}","lang":"json","description":"Shows installation, configuration with plugins array and sort preset, and example formatting of package.json."},"warnings":[{"fix":"Upgrade Prettier to ^3.0.3 (or higher).","message":"Plugin only works with Prettier v3. Using with Prettier v2 will throw an error or silently fail.","severity":"gotcha","affected_versions":">=0.22.0"},{"fix":"Check script ordering after upgrade; may need to adjust script names if custom ordering changed.","message":"v0.22.0 changed handling of pre/post scripts: now sorts them together with base script, except for prepare, preprepare, postpare, postprepare.","severity":"breaking","affected_versions":">=0.22.0"},{"fix":"Use ESM import syntax or rely on Prettier plugin resolution; avoid require('prettier-plugin-pkg').","message":"CJS require() import pattern is deprecated; package uses ESM exports only (since v0.21.2).","severity":"deprecated","affected_versions":">=0.21.2"},{"fix":"Use packageSortOrder to explicitly place custom keys or set packageIgnoreSort for specific keys.","message":"The plugin sorts unknown keys alphabetically and appends them. If you have a custom top-level key not in the known list, it may end up in unexpected position.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run `npm install --save-dev prettier prettier-plugin-pkg` and ensure both are in devDependencies.","cause":"Plugin not installed or not in node_modules; or Prettier is not installed.","error":"Error: Cannot find module 'prettier-plugin-pkg'"},{"fix":"Upgrade Prettier to 3.x. Check that plugin is installed and listed in plugins array.","cause":"Prettier version mismatch (requires Prettier ^3.0.3) or plugin not loaded.","error":"Error: prettier-plugin-pkg: Your configuration's plugin entry 'prettier-plugin-pkg' is not working"},{"fix":"Update prettier-plugin-pkg to v0.21.0+ with `npm install prettier-plugin-pkg@latest`.","cause":"Using old plugin version (<0.21.0) that does not support packageSortOrderPreset.","error":"Warning: Ignoring unknown option 'packageSortOrderPreset'"},{"fix":"Remove direct require; use only via Prettier configuration.","cause":"Attempting to use require('prettier-plugin-pkg') directly; the module does not export a function for sorting.","error":"TypeError: Cannot read properties of undefined (reading 'sort')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}