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
🌐
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
🌐
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.
🌐
Curious Programmer
curiousprogrammer.dev › scribble › how-to-sort-your-package-json-dependencies
How to sort your package.json dependencies | Curious Programmer
Want to keep your package.json dependencies sorted alphabetically without installing an extra package? Here's a quick npm command to help yo...
🌐
Alainnicolas
alainnicolas.fr › en › blog › sort-your-dependencies
Sort your dependencies! | Alain Nicolas
January 2, 2026 - It allows you to sort the package.json file alphabetically based on package names. This ensures consistency in the order of dependencies, making the file more readable and maintainable.
🌐
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 an Object or package.json based on the well-known package.json keys - keithamus/sort-package-json
Author   keithamus
🌐
GitHub
github.com › npm › npm › issues › 9219
Propose a order convention for `package.json` properties. · Issue #9219 · npm/npm
August 10, 2015 - I'd love an array of the standard ... package.json should be in, or at-least the first couple. var packageOrder = [ 'name', 'version', 'description', 'main', 'bin', 'scripts', 'keywords', 'homepage', 'bugs', 'license', 'author', 'contributors', ...
Published   Aug 10, 2015
Find elsewhere
🌐
GitHub
github.com › zetlen › eslint-plugin-package-json › blob › master › docs › rules › sort-collections.md
eslint-plugin-package-json/docs/rules/sort-collections.md at main ...
Whenever npm changes package dependencies through npm install, it sorts (lexicographically) all dependencies in package.json. However, developers will manually update package.json and accidentally leave dependencies out of order.
Author   JoshuaKGoldberg
🌐
Google Groups
groups.google.com › g › narwhaljs › c › m5viWZXP_BA
dependency order in package.json
There is no overlay or "depender before dependee" precedence behavior of such modules. For system-packages, order does matter. The package sorting algorithm performs a topological, prefix sort of the dependency graph, and visits nodes in the order they appear in the dependency lists of each module.
🌐
npm
npmjs.com › package › fixpack
fixpack - npm
It will re-write your package.json file as follows: name first · description second · version third · author fourth · all other keys in alphabetical order · dependencies and devDependencies sorted alphabetically · newline at the end of the file · It will warn you if any of these are missing: description ·
      » npm install fixpack
    
Published   Jan 03, 2021
Version   4.0.0
Author   Henrik Joreteg
🌐
Yarn
yarnpkg.com › package
sort-package-json
Yarn is a package manager that doubles down as project manager. Whether you work on simple projects or industry monorepos, whether you're an open source developer or an enterprise user, Yarn has your back · First package manager built specifically around workspaces, Yarn lets you split your ...
🌐
npm
npmjs.com › sort-package-json-scripts
sort-package-json-scripts - npm
2 Dependents · 36 Versions · Sort package.json scripts field following npm lifecycle scripts order 排序 package.json scripts 欄位,遵循 npm 生命週期腳本順序 · A better sort package.json scripts utility. By default, it follows npm lifecycle scripts order for better readability.
      » npm install sort-package-json-scripts
    
🌐
GitHub
github.com › biomejs › biome › discussions › 7048
package.json sorted dependencies · biomejs/biome · Discussion #7048
Like; eslint-plugin-package-json's sort-collections prettier-plugin-packagejson, which uses sort-package-json Sherif's unordered-dependencies eslint-plugin-package-json-dependencies's alphabeticall...
Author   biomejs
🌐
Yarn
classic.yarnpkg.com › en › package › sort-package-json
sort-package-json
Dependencies · 0 · DevDependencies · 0 · Packages · see package.json
🌐
Wanago
wanago.io › home › keeping your dependencies in order when using npm.
Keeping your dependencies in order when using NPM. - wanago.io - Marcin Wanago Blog
May 21, 2018 - This separation allows us to set some rules on how do we want to deal with new versions of our dependencies. In our package.json file we have a property called dependencies. It is an object that will hold information about the packages, that your project depends on, therefore they will be installed with npm install command.
🌐
GitHub
github.com › kilianc › npm-sort
GitHub - kilianc/npm-sort: Sorts your npm dependencies
$ cd my_node_project $ npm-sort · From · "dependencies": { "koa": "~0.1.2", "koa-router": "~1.6.1", "co": "~3.0.1", "request": "~2.30.0", "colors": "~0.6.2", "async": "~0.2.9", "JSONStream": "~0.7.1", "qs": "~0.6.6", "event-stream": "~3.0.20", "json-array-stream": "~0.1.2", "koa-jsonp": "~0.0.3", "koa-mount": "~1.2.2", "koa-compress": "~1.0.0", "koa-favicon": "~1.0.1", "koa-response-time": "~1.0.1", "debug": "~0.7.4", "co-body": "0.0.1", "nano": "~4.2.1" }, to ·
Starred by 46 users
Forked by 4 users
Languages   JavaScript