better-sort-package-json
Sorts a package.json
file following these rules:
- The root properties follow the order they appear in the NPM
package.json
specification, then the Node.JS fields, and finally all the other properties. - The string arrays in the root, such as
keywords
and files
, are sorted alphabetically. - The properties in
script
are sorted alphabetically but placing the "pre" and "post" scripts along with before and after its target. - All other nested objects have their properties sorted in alphabetical order.
Motivation and prior art
Other packages like sort-package-json
already exist but there is no way to customize the sorting rule applied and I was looking for a simpler set matching current practices at Hemi.
CLI
This package can be installed globally, then used to sort package.json
files:
npm install --global better-sort-package-json
better-sort-package-json <path-to-a-package-json-file-to-sort>
In addition, it can be used without installing it:
npx better-sort-package-json <path-to-a-package-json-file-to-sort>
API
Install the package:
npm install better-sort-package-json
Then use it to sort the contents of a package.json
:
import * as fs from "node:fs";
import { sortPackageJson } from "better-sort-package-json";
fs.writeFileSync(path, sortPackageJson(fs.readFileSync(path, "utf8")));
Automatically sort on commit
To i.e. let lint-staged
take care of sorting the package.json
automatically, but without altering the formatting done by Prettier
, this configuration can be used:
{
"*.json,!package.json": ["prettier --write"],
"package.json": ["better-sort-package-json", "prettier --write"]
}