New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

sort-package-json-rs

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-package-json-rs

Node.js bindings and CLI for OXC(oxfmt)'s package.json sorter

latest
Source
npmnpm
Version
0.0.5
Version published
Maintainers
1
Created
Source

sort-package-json-rs

Node.js bindings and CLI for OXC(oxfmt)'s package.json sorter.

npm version npm downloads bundle size install size codecov license

Introduction

oxc-project/sort-package-json is a high-performance Rust implementation for sorting package.json files, designed as a replacement for keithamus/sort-package-json. It powers the “Sort package.json fields” feature in oxfmt.

This package provides Node.js bindings for that implementation, along with a CLI that is similar to the original sort-package-json.

It can be used independently of oxfmt as a drop-in replacement for the original sort-package-json package, or integrated directly into your own tools and workflows in Node.js compatible environments.

Note on Compatibility: oxc-project/sort-package-json is not strictly compatible with the original sort-package-json npm package. It uses a different sorting algorithm and field order, so the output may differ from the original package.

Installation

# npm
npm install sort-package-json-rs

# yarn
yarn add sort-package-json-rs

# pnpm
pnpm add sort-package-json-rs

# bun
bun install sort-package-json-rs

# deno
deno install npm:sort-package-json-rs

CLI

Run it via npx, pnpm dlx, bunx, or deno run:

# npm
npx sort-package-json-rs@latest

# pnpm
pnpm dlx sort-package-json-rs@latest

# bun
bunx sort-package-json-rs@latest

# deno
deno run -A npm:sort-package-json-rs@latest

Usage Options

$ sort-package-json-rs --help
sort-package-json-rs/0.0.2

Usage:
  $ sort-package-json-rs [...files]

Commands:
  [...files]  Sort package.json files

For more info, run any command with the `--help` flag:
  $ sort-package-json-rs --help

Options:
  -c, --check          Check if files are sorted without modifying them 
  -q, --quiet          Don't output success messages 
  --stdin              Read package.json from stdin 
  -i, --ignore <glob>  Glob patterns to ignore (default: node_modules/**)
  -v, --version        Display version number 
  -h, --help           Display this message 

API

Basic Usage

import { sortPackageJson } from 'sort-package-json-rs';

const unsorted = {
  dependencies: {
    'react': '^18.0.0',
    'typescript': '^5.0.0',
    'lodash': '^4.17.0'
  },
  name: 'my-package',
  version: '1.0.0',
  scripts: {
    'build': 'tsc',
    'test': 'vitest'
  }
};

// Use default options (pretty-printed)
const sorted = sortPackageJson(JSON.stringify(unsorted));
console.log(sorted);

// Or with custom options
const compactSorted = sortPackageJson(JSON.stringify(unsorted), {
  pretty: false,
  sortScripts: true
});
console.log(compactSorted);

TypeScript Types

/** Options for controlling JSON formatting when sorting */
export interface SortOptions {
  /** Whether to pretty-print the output JSON */
  pretty: boolean
  /** Whether to sort the scripts field alphabetically */
  sortScripts: boolean
}

/**
 * Sorts a package.json string with optional custom options
 * If options is not provided, uses default options (pretty-printed)
 */
export declare function sortPackageJson(input: string, options?: SortOptions | undefined | null): string

Benchmarks

Using tests/index.bench.ts with vitest bench:

Test CaseOriginal (hz)RS Binding (hz)Speedup
Small package.json15,719.8025,073.371.60x ⚡
Already sorted package.json17,005.2026,249.621.54x ⚡
Minimal package.json139,344.74907,643.816.51x ⚡
Large package.json42,820.9472,237.261.69x ⚡

Executed on an MacBook Air (M1, 2020, 16GB RAM) with Node.js v23.7.0.

Credits

Some code and tests are adapted from keithamus/sort-package-json and oxc-project/sort-package-json.

License

MIT License © maxchang3

🤖 auto updated with automd

Keywords

docs

FAQs

Package last updated on 14 Mar 2026

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