The sort-package-json package sorts not only dependencies and devDependencies, but other keys as well. I know the original questions didn't ask about the other keys, but I think it's cool to have all keys sorted.

You can simply run:

npx sort-package-json

Example from the package page:

$ cd my-project

$ cat package.json
{
  "dependencies": {
    "sort-package-json": "1.0.0",
    "sort-object-keys": "1.0.0"
  },
  "version": "1.0.0",
  "name": "my-awesome-project"
}
 
$ npx sort-package-json
package.json is sorted!
 
$ cat package.json
{
  "name": "my-awesome-project",
  "version": "1.0.0",
  "dependencies": {
    "sort-object-keys": "1.0.0",
    "sort-package-json": "1.0.0"
  }
}

This does not remove the trailing newline like the npm-sort package mentioned by Wolfgang.

Multiple files

$ sort-package-json "my-package/package.json" "other-package/package.json"

$ sort-package-json "package.json" "packages/*/package.json"
Answer from Matias Kinnunen on Stack Overflow
🌐
GitHub
github.com › keithamus › sort-package-json
GitHub - keithamus/sort-package-json: Sort an Object or package.json based on the well-known package.json keys · GitHub
$ sort-package-json "**/package.json" --check foo/package.json bar/package.json Found 5 files. 3 files were not sorted. 2 files were already sorted. In order to silence any successful output, you can run CLI with the --quiet flag (or -q). This will stop the CLI from outputting if it runs successfully, but won't effect error messages and the exit code. $ sort-package-json "**/package.json" --check --quiet $ sort-package-json "**/package.json" --quiet
Author   keithamus
🌐
npm
npmjs.com › package › sort-package-json
sort-package-json - npm
Latest version: 3.6.1, last published: a month ago. Start using sort-package-json in your project by running `npm i sort-package-json`. There are 619 other projects in the npm registry using sort-package-json.
      » npm install sort-package-json
    
Published   Jan 25, 2026
Version   3.6.1
Author   Keith Cirkel
People also ask

Why should I sort JSON keys?
Sorting keys makes data easier to read for humans and ensures deterministic file structures, which helps prevent merge conflicts in version control systems like Git.
🌐
codeshack.io
codeshack.io › home › tools › json sorter
JSON Sorter - Sort JSON keys and values online
Is my JSON data secure?
Yes, absolutely. This tool runs 100% on the client-side using JavaScript. Your JSON data never leaves your browser and is never sent to any external server.
🌐
codeshack.io
codeshack.io › home › tools › json sorter
JSON Sorter - Sort JSON keys and values online
What happens if my JSON has syntax errors?
The tool validates your input before processing. If your JSON is invalid, the output area will remain empty. You must correct syntax errors before the tool can sort the data.
🌐
codeshack.io
codeshack.io › home › tools › json sorter
JSON Sorter - Sort JSON keys and values online
🌐
Codsen
codsen.com › os › json-sort-cli
json-sort-cli open source npm package | Codsen
It sorts JSON files deeply. That is, it does not matter is it’s a plain object within array within array within a plain object — all objects no matter how deep, will be detected and sorted. If this tool processes any package.json files (put -p to disable this), it will first deep-sort alphabetically, then use format-package (npm) to arrange keys according to a commonly agreed format — name at the top, depedencies at the bottom etc.
🌐
CodeShack
codeshack.io › home › tools › json sorter
JSON Sorter - Sort JSON keys and values online
Sort JSON keys and values, rearrange your JSON text with this online tool, alphabetical and numerical sort.
Find elsewhere
🌐
DEV Community
dev.to › cbillowes › how-to-sort-your-packagejson-dependencies-1gi1
How to sort your package.json dependencies - DEV Community
April 18, 2022 - "example" is just the name of a package. You can use it verbatim because it will most likely not be in your package.json. Your packages are sorted by running npm remove even if the package "example" doesn't exist.
🌐
CodeSandbox
codesandbox.io › examples › package › sort-package-json
sort-package-json examples - CodeSandbox
Use this online sort-package-json playground to view and fork sort-package-json example apps and templates on CodeSandbox.
🌐
GitHub
github.com › oxc-project › sort-package-json
GitHub - oxc-project/sort-package-json
A Rust implementation that sorts package.json files according to well-established npm conventions.
Starred by 17 users
Forked by 2 users
Languages   Rust 96.5% | Just 3.5%
🌐
Snyk
snyk.io › advisor › sort-package-json › sort-package-json code examples
Top 5 sort-package-json Code Examples | Snyk
terascope / teraslice / packages / scripts / src / helpers / packages.ts View on Github · function getSortedPkgJSON(pkgInfo: T): T { return fastCloneDeep(sortPackageJson(pkgInfo)); } ... }, devDependencies: { [cli.name]: cli.version, }, }); if (template !== 'js') { pkg = merge(pkg, { klap: { example: `public/index.${template}` } }); if (template === 'ts' || template === 'tsx') { pkg = merge(pkg, { types: 'dist/types', scripts: { postbuild: 'tsc -p tsconfig.json' }, devDependencies: { typescript: cli.devDependencies['typescript'] }, }); } } await write('./package.json', JSON.stringify(sort(pkg), null, ' ')); info('\t- wrote ./package.json'); return pkg; };
🌐
Tessl
tessl.io › registry › tessl › npm-sort-package-json › 2.4.0
2.4.0 • npm-sort-package-json • tessl • Registry • Tessl
January 29, 2026 - # Sort current package.json sort-package-json # Check if files are sorted sort-package-json --check sort-package-json "**/*.json" --check # Sort multiple files with ignore patterns sort-package-json "packages/*/package.json" --ignore "packages/legacy/*" # Use with stdin/stdout cat package.json | sort-package-json --stdin > sorted-package.json # Quiet mode (suppress success messages) sort-package-json --quiet · The CLI returns different exit codes to indicate various outcomes: ... # Check if files are sorted (exits with 1 if any are unsorted) sort-package-json --check echo $? # Will be 1 if files need sorting # Successful sorting (exits with 0) sort-package-json package.json echo $? # Will be 0 on success # Error case - no files found (exits with 2) sort-package-json "nonexistent/*.json" echo $? # Will be 2
🌐
npm
npmjs.com › sort-package-json-scripts
sort-package-json-scripts - npm
Sort package.json scripts field ... a day ago. Start using sort-package-json-scripts in your project by running `npm i sort-package-json-scripts`. ......
      » npm install sort-package-json-scripts
    
🌐
GitHub
github.com › unional › vscode-sort-package-json
GitHub - unional/vscode-sort-package-json: Sort package.json VSCode extension
A Visual Studio Code extension to sort package.json with sort-package-json.
Starred by 31 users
Forked by 5 users
Languages   TypeScript 93.3% | JavaScript 6.7%
🌐
Sebastian De Deyne
sebastiandedeyne.com › cleaning-up-package-json-keys
Cleaning up package.json keys | Sebastian De Deyne
Luckily there's an npm package for everything. With the sort-package-json package you can go sort your package.jsons in a logical order.
🌐
Alainnicolas
alainnicolas.fr › en › blog › sort-your-dependencies
Sort your dependencies! | Alain Nicolas
January 2, 2026 - Using it is simple! Just run npx sort-package-json and you’re done!
🌐
npm
npmjs.com › package › sort-json
sort-json - npm
Takes a json-file and return a copy of the same file, but sorted. Latest version: 2.0.1, last published: 4 years ago. Start using sort-json in your project by running `npm i sort-json`. There are 58 other projects in the npm registry using sort-json.
      » npm install sort-json
    
Published   Feb 12, 2022
Version   2.0.1
Author   David Björklund
🌐
Quora
quora.com › Why-are-fields-in-package-JSON-not-sorted-alphabetically
Why are fields in package.JSON not sorted alphabetically? - Quora
Answer: a) Because whatever tool you are using didn’t sort them. b) You probably don’t want them sorted anyway. Putting the fields that humans need to look at earlier makes a lot of sense. c) Because it doesn’t matter, the order of keys in a JSON object does not mean anything.