Registry / devops / prettier-plugin-pkg

prettier-plugin-pkg

JSON →
library0.22.1jsnpmunverified

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`.

npm install prettier-plugin-pkg
INSTALL
IMPORT
SIG · PRETTIER-PLUGIN-PK
P
prettier-plugin-pkg
devopsjavascriptv0.22.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

prettier-plugin-pkg
plugins: ['prettier-plugin-pkg'] in .prettierrc
const plugin = require('prettier-plugin-pkg')
This is a Prettier plugin, not a Node module to import directly. Use it by adding to prettier config plugins array.
packageSortOrder
// .prettierrc { "packageSortOrder": ["name", "version", "description"] }
// incorrect: using as import import { packageSortOrder } from 'prettier-plugin-pkg'
Option available since v0.21.0. Set in Prettier config, not imported.
packageIgnoreSort
// .prettierrc { "packageIgnoreSort": true }
Option added in v0.21.0. Set in Prettier config to skip sorting specific keys.

Shows installation, configuration with plugins array and sort preset, and example formatting of package.json.

// 1. Install // npm install --save-dev prettier prettier-plugin-pkg // 2. Create .prettierrc.json { "plugins": ["prettier-plugin-pkg"], "packageSortOrderPreset": "npm" } // 3. Format package.json // npx prettier --write package.json // Example package.json input: { "description": "A sample package", "name": "my-package" } // Output: { "name": "my-package", "description": "A sample package" }
Debug
Known issues
gotchaPlugin only works with Prettier v3. Using with Prettier v2 will throw an error or silently fail.
fix
Upgrade Prettier to ^3.0.3 (or higher).
affects: >=0.22.0
breakingv0.22.0 changed handling of pre/post scripts: now sorts them together with base script, except for prepare, preprepare, postpare, postprepare.
fix
Check script ordering after upgrade; may need to adjust script names if custom ordering changed.
affects: >=0.22.0
deprecatedCJS require() import pattern is deprecated; package uses ESM exports only (since v0.21.2).
fix
Use ESM import syntax or rely on Prettier plugin resolution; avoid require('prettier-plugin-pkg').
affects: >=0.21.2
gotchaThe 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.
fix
Use packageSortOrder to explicitly place custom keys or set packageIgnoreSort for specific keys.
affects: *
Errors
Common errors & fixes
Error: Cannot find module 'prettier-plugin-pkg'
Plugin not installed or not in node_modules; or Prettier is not installed.
fix
Run `npm install --save-dev prettier prettier-plugin-pkg` and ensure both are in devDependencies.
Error: prettier-plugin-pkg: Your configuration's plugin entry 'prettier-plugin-pkg' is not working
Prettier version mismatch (requires Prettier ^3.0.3) or plugin not loaded.
fix
Upgrade Prettier to 3.x. Check that plugin is installed and listed in plugins array.
Warning: Ignoring unknown option 'packageSortOrderPreset'
Using old plugin version (<0.21.0) that does not support packageSortOrderPreset.
fix
Update prettier-plugin-pkg to v0.21.0+ with `npm install prettier-plugin-pkg@latest`.
TypeError: Cannot read properties of undefined (reading 'sort')
Attempting to use require('prettier-plugin-pkg') directly; the module does not export a function for sorting.
fix
Remove direct require; use only via Prettier configuration.
Upgrade
Version history
0.22.1latest on npm
Audit
Dependencies
prettierrequiredpeer dependency: plugin requires Prettier 3.x to load and format
Agent activity
2 hits · last 30 days
node
2
Resources
prettier-plugin-pkg — npm install prettier-plugin-pkg · libregistry