prettier-plugin-pkgsort

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

Prettier plugin that sorts package.json keys and fields using prettier-package-json. Current stable version 0.3.0 requires Prettier ^3.0.0 as a peer dependency. It provides consistent key ordering, sensible script sorting, expand/contract for author/contributors, and file field filtering/sorting. Ships TypeScript types. Released under MIT license.

error Error: Cannot find module 'prettier-plugin-pkgsort'
cause Missing package installation or incorrect module resolution
fix
Run npm install prettier-plugin-pkgsort --save-dev and ensure node_modules is present. Use require.resolve in config.
error TypeError: Cannot read properties of undefined (reading 'plugins')
cause Prettier config module.exports returns undefined or invalid object
fix
Ensure config exports an object correctly: module.exports = { plugins: [...] };
error Error: Could not resolve 'prettier-plugin-pkgsort' from '...'
cause Using string literal instead of require.resolve in CommonJS config
fix
Change plugins: ['prettier-plugin-pkgsort'] to plugins: [require.resolve('prettier-plugin-pkgsort')]
breaking Version 0.2.0 dropped support for Prettier 2; requires Prettier ^3.0.0.
fix Upgrade Prettier to version 3.x. If stuck on Prettier 2, use version 0.1.x.
deprecated CommonJS require() of the package directly may not work in v0.2.1+. Package became ESM-only.
fix Use import expression or require.resolve in CommonJS configs: plugins: [require.resolve('prettier-plugin-pkgsort')]
gotcha Options like `pkgsortExpandUsers` and `pkgsortKeyOrder` must be placed in Prettier config, not in plugin options.
fix Add options directly to the Prettier config object with the `pkgsort` prefix. Do not nest under plugin options.
gotcha Plugin identifies package.json files by filename only. If your file has a different name (e.g., .package.json), it will not be processed.
fix Rename file to package.json or use Prettier's file match patterns to include it.
npm install prettier-plugin-pkgsort
yarn add prettier-plugin-pkgsort
pnpm add prettier-plugin-pkgsort

Configure Prettier to use prettier-plugin-pkgsort with custom expandUsers and keyOrder options.

// .prettierrc.cjs (CommonJS)
module.exports = {
  plugins: [require.resolve('prettier-plugin-pkgsort')],
  pkgsortExpandUsers: true,
  pkgsortKeyOrder: ['name', 'version', 'description', 'main', 'scripts', 'dependencies', 'devDependencies'],
};