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 › prettier-plugin-sort-json
prettier-plugin-sort-json - npm
Start using prettier-plugin-sort-json in your project by running `npm i prettier-plugin-sort-json`. There are 72 other projects in the npm registry using prettier-plugin-sort-json.
      » npm install prettier-plugin-sort-json
    
🌐
npm
npmjs.com › package › prettier-plugin-packagejson
prettier-plugin-packagejson - npm
A Prettier plugin to sort the keys of a package.json file using sort-package-json.
      » npm install prettier-plugin-packagejson
    
🌐
GitHub
github.com › matzkoh › prettier-plugin-packagejson
GitHub - matzkoh/prettier-plugin-packagejson: Prettier plugin for package.json
A Prettier plugin to sort the keys of a package.json file using sort-package-json.
Starred by 382 users
Forked by 11 users
Languages   JavaScript 90.7% | TypeScript 9.3%
🌐
npm
npmjs.com › package › prettier-plugin-json-sort
prettier-plugin-json-sort - npm
Latest version: 0.0.2, last published: 2 years ago. Start using prettier-plugin-json-sort in your project by running `npm i prettier-plugin-json-sort`. There are 4 other projects in the npm registry using prettier-plugin-json-sort.
      » npm install prettier-plugin-json-sort
    
Published   Dec 09, 2023
Version   0.0.2
Author   Soybean
🌐
GitHub
github.com › prettier-plugins › sort-package-json
GitHub - prettier-plugins/sort-package-json: A prettier plugin thats sorts your package.json using the sort-package-json npm package
prettier-plugin-sort-package-json is a prettier plugin that sorts your package.json using the sort-package-json npm package.
Author   prettier-plugins
🌐
GitHub
github.com › Gudahtt › prettier-plugin-sort-json
GitHub - Gudahtt/prettier-plugin-sort-json: A plugin for Prettier that sorts JSON files by property name.
A plugin for Prettier that sorts JSON files by property name. - Gudahtt/prettier-plugin-sort-json
Starred by 122 users
Forked by 16 users
Languages   TypeScript 95.0% | JavaScript 4.6% | Shell 0.4%
🌐
npm
npmjs.com › package › sort-package-json
sort-package-json - npm
Prettier · remark · semantic-release · stylelint · Tap · xojs · Alphabetically ordered. The package.json file can be sorted automatically before committing. npm install husky lint-staged --save-dev npm pkg set scripts.prepare="husky install" npm run prepare npx husky add .husky/pre-commit "npx lint-staged" Add the following to your package.json file ·
      » npm install sort-package-json
    
Published   Jan 25, 2026
Version   3.6.1
Author   Keith Cirkel
🌐
GitHub
github.com › prettier › prettier › issues › 4040
Idea: sort package.json keys · Issue #4040 · prettier/prettier
February 25, 2018 - It would be really cool to integrate something like this directly inside Prettier.
Published   Feb 25, 2018
Find elsewhere
🌐
GitHub
github.com › so1ve › prettier-plugin-pkgsort
GitHub - so1ve/prettier-plugin-pkgsort: Use prettier-package-json to sort your package.json.
Use prettier-package-json to sort your package.json. ... $ npm install prettier-plugin-pkgsort -D $ yarn add prettier-plugin-pkgsort -D $ pnpm add prettier-plugin-pkgsort -D
Author   so1ve
🌐
GitHub
github.com › trivago › prettier-plugin-sort-imports › blob › main › package.json
prettier-plugin-sort-imports/package.json at main · trivago/prettier-plugin-sort-imports
A prettier plugin to sort imports in typescript and javascript files by the provided RegEx order. - prettier-plugin-sort-imports/package.json at main · trivago/prettier-plugin-sort-imports
Author   trivago
🌐
npm
npmjs.com › package › prettier-package-json
prettier-package-json - npm
By default it uses opinionated defaults but can be configured to your individual needs. Keys in package.json will be sorted in an opinionated order but may be configured to your own preferences.
      » npm install prettier-package-json
    
Published   Dec 23, 2022
Version   2.8.0
Author   Cameron Hunter
🌐
Socket
socket.dev › npm › package › prettier-plugin-sort-json
prettier-plugin-sort-json - npm Package Security Analysis - ...
Prettier plugin to sort JSON files alphanumerically by key. Version: 4.0.0 was published by gudahtt. Start using Socket to analyze prettier-plugin-sor...
🌐
Alibaba Cloud
developer.aliyun.com › mirror › npm › package › prettier-plugin-sort-json
Package - prettier-plugin-sort-json
This module requires an LTS Node version (v10.0.0+), and prettier v2.0.0+. ... This plugin adds a JSON preprocessor that will sort JSON files containing a JSON object alphanumerically by key. JSON files containing Arrays or other non-Object values are skipped.
🌐
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
Prettier · remark · semantic-release · stylelint · Tap · xojs · Alphabetically ordered. The package.json file can be sorted automatically before committing. npm install husky lint-staged --save-dev npm pkg set scripts.prepare="husky install" npm run prepare npx husky add .husky/pre-commit "npx lint-staged" Add the following to your package.json file ·
Author   keithamus
🌐
npm
npmjs.com › package › prettier-plugin-sort-imports
prettier-plugin-sort-imports - npm
A prettier plugin that sorts import statements by either their length or alphabetically.
      » npm install prettier-plugin-sort-imports
    
Published   Feb 17, 2026
Version   1.8.11
Author   Sander Ronde
🌐
Npm
npm.io › package › prettier-plugin-sort-json
Prettier-plugin-sort-json NPM | npm.io
No configuration is necessary; Prettier will detect this plugin in your dependencies and use it automatically. There are some configuration options available (described below), but they are all optional. This plugin adds a JSON preprocessor that will sort JSON files alphanumerically by key.