Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

better-sort-package-json

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-sort-package-json

Sort a package.json file following the NPM docs

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

better-sort-package-json

Sorts a package.json file following these rules:

  1. 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.
  2. The string arrays in the root, such as keywords and files, are sorted alphabetically.
  3. The properties in script are sorted alphabetically but placing the "pre" and "post" scripts along with before and after its target.
  4. 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"]
}

Keywords

FAQs

Package last updated on 18 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc