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
If you're using a JavaScript Prettier configuration file, all of this escaping can be avoided by using JSON.stringify to create the jsonSortOrder string from an object with RegExp literal keys. For example, you could create the earlier example JSON sort order string like this:
      » npm install prettier-plugin-sort-json
    
🌐
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%
🌐
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.
If you're using a JavaScript Prettier configuration file, all of this escaping can be avoided by using JSON.stringify to create the jsonSortOrder string from an object with RegExp literal keys. For example, you could create the earlier example JSON sort order string like this:
Starred by 122 users
Forked by 16 users
Languages   TypeScript 95.0% | JavaScript 4.6% | Shell 0.4%
🌐
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 › 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
# using npm npm i -D prettier-plugin-sort-package-json # using yarn yarn add -D prettier-plugin-sort-package-json
Author   prettier-plugins
🌐
CodeSandbox
codesandbox.io › examples › package › prettier-plugin-sort-json
prettier-plugin-sort-json examples - CodeSandbox
AboutPrettier plugin to sort JSON files alphanumerically by key268,357Weekly Downloads · Latest version4.1.1 · LicenseMIT · External Links · github.com/Gudahtt/prettier-plugin-sort-json ·
🌐
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
"homepage": "https://github.com/trivago/prettier-plugin-sort-imports#readme", "scripts": { "prepare": "yarn run compile", "compile": "tsc", "preexample": "yarn run compile", "example": "prettier --config ./examples/.prettierrc", "test": "vitest run", "type-check": "tsc --noEmit", "prepublishOnly": "npm run compile && npm run test", "prettify": "yarn prettier --write ."
Author   trivago
🌐
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
🌐
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
🌐
npm
npmjs.com › package › prettier-package-json
prettier-package-json - npm
prettier-package-json is a JSON formatter inspired by prettier. It removes all original styling and ensures that the outputted package.json conforms to a consistent style. 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
🌐
GitHub
github.com › Gudahtt › prettier-plugin-sort-json › tree › main-v2
GitHub - Gudahtt/prettier-plugin-sort-json at main-v2
These regular expressions are ... example JSON sort order string would would be "{ \"placeThisFirst\": null, \"/^[^\\\\d+]/\": \"lexical\", \"/^\\\\d+/\": \"numeric\" }....
Starred by 117 users
Forked by 15 users
Languages   TypeScript 94.9% | JavaScript 4.7% | Shell 0.4%
🌐
GitHub
github.com › soybeanjs › prettier-plugin-json-sort
GitHub - soybeanjs/prettier-plugin-json-sort: A prettier plugin to sort package.json and tsconfig.json.
A prettier plugin to sort package.json and tsconfig.json. - soybeanjs/prettier-plugin-json-sort
Author   soybeanjs
🌐
npm
npmjs.com › package › prettier-plugin-sort-package-json
prettier-plugin-sort-package-json - npm
If you need to sort a package.json-like file, you can use the custom parser option: { "plugins": ["prettier-plugin-sort-package-json"], "overrides": [ { "files": ["pkg.json", "template.json"], "options": { "parser": "sort-package-json" } } ] }
      » npm install prettier-plugin-sort-package-json
    
🌐
Socket
socket.dev › npm › package › prettier-plugin-sort-json
prettier-plugin-sort-json - npm Package Security Analysis - ...
These regular expressions are ... example JSON sort order string would would be "{ \"placeThisFirst\": null, \"/^[^\\\\d+]/\": \"lexical\", \"/^\\\\d+/\": \"numeric\" }....
🌐
Twitter
twitter.com › PrettierCode › status › 1194342261682630656
Do you sort your package.json?
JavaScript is not available · We’ve detected that JavaScript is disabled in this browser. Please enable JavaScript or switch to a supported browser to continue using twitter.com. You can see a list of supported browsers in our Help Center · Help Center · Terms of Service Privacy Policy ...
🌐
GitHub
github.com › Gudahtt › prettier-plugin-sort-json › releases
Releases · Gudahtt/prettier-plugin-sort-json
However, if your project needs ... example, you could set this as your prettierrc.js file: { jsonSortOrder: JSON.stringify({ '/^[Aa]/': 'none', '/^[Bb]/': 'none', '/^[Cc]/': 'none', ......
Author   Gudahtt
🌐
Npm
npm.io › package › prettier-plugin-sorted-json
Prettier-plugin-sorted-json NPM | npm.io
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.