Prettier for
package.json.
I like my package.json files to be nearly organized, making sure that
each property is in the same order as listed in the official npm package.json documentation,
including nested objects like author, contributors, directories
and more.
This tools allows me to stop doing that manually. I just
%!pretty-package-json from Vim and call it a day.
Format given file, stdin or package.json otherwise according to
rules, and print it to stdout:
pretty-package-json whatever.json
pretty-package-json < whatever.json
pretty-package-jsonFormat given file or package.json otherwise, and overwrite it:
pretty-package-json --write whatever.json
pretty-package-json --writeThe top-level keys are sorted as defined in the documentation.
Reveal top-level keys
nameversiondescriptionkeywordshomepagebugslicenseauthorcontributorsfundingfilesmainbrowserbinmandirectoriesrepositoryscriptsconfigdependenciesdevDependenciespeerDependenciespeerDependenciesMetabundledDependenciesoptionalDependenciesoverridesenginesoscpuprivatepublishConfigworkspaces
Those are automatically fetched from the documentation. But for nested object keys, there's extra rules that need to be defined manually (see below).
We also support extra keys that are not part of the npm
documentation but are commonly used in the ecosystem, like type,
module, exports, and types.
Anywhere we're sorting according to a predefined order, unknown keys will be added at the end in the same order they were found.
Empty arrays and objects are removed.
If the homepage and bugs match the one that can be derived from
the repository by hosted-git-info,
the keys will be removed.
For example:
{
"homepage": "https://github.com/valeriangalliat/pretty-package-json",
"bugs": "https://github.com/valeriangalliat/pretty-package-json/issues",
"repository": "valeriangalliat/pretty-package-json"
}Here the homepage and bugs are redundant and it will be rewritten
as:
{
"repository": "valeriangalliat/pretty-package-json"
}Converted to the short form name <email> (url) unless peopleFormat
is set to object, then it's sorted as:
nameemailurl
If contains only a single script matching the package name, it's flattened as a string.
bindoclibman
Convert to the shortest form supported, e.g. use/repo if hosted on
GitHub, gitlab:user/repo, a full URL, or otherwise sorted as:
typeurldirectory
Sorted alphabetically unless sortScripts is set to false.
Sorted alphabetically like npm does by default when populating those objects.
nodenpm
$schema: for JSON Schema validation.type: Node.js input type, e.g."type": "module"or"type": "commonjs".module: Node.js legacy method that allowed to define the ES module entry point, as opposed to CommonJS inmain.exports: Node.js field allowing to define hybrid entry points.types: TypeScript types.
To confirm this list and see the non-npm top-level keys that were manually added, run:
git diff --no-index npm-keys.json keys.json