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
January 10, 2025 - Prettier plugin to sort JSON files alphanumerically by key. Latest version: 4.2.0, last published: 2 months ago. Start using prettier-plugin-sort-json in your project by running `npm i prettier-plugin-sort-json`. There are 72 other projects ...
      » npm install prettier-plugin-sort-json
    
🌐
npm
npmjs.com › package › prettier-plugin-packagejson
prettier-plugin-packagejson - npm
January 20, 2026 - 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%
🌐
Socket
socket.dev › npm › package › prettier-plugin-packagejson › overview › 2.4.7
prettier-plugin-packagejson - npm Package Security Analysis ...
December 3, 2023 - Prettier package.json plugin to make the order of properties nice. ... A Prettier plugin to sort the keys of a package.json file using sort-package-json.
Find elsewhere
🌐
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 Trends
npmtrends.com › eslint-plugin-json-vs-eslint-plugin-json-files-vs-npm-package-json-lint-vs-package-json-validator-vs-prettier-plugin-package-vs-sort-package-json
eslint-plugin-json vs eslint-plugin-json-files vs npm-package-json-lint vs package-json-validator vs prettier-plugin-package vs sort-package-json | npm trends
Comparing trends for eslint-plugin-json ... which has 109,378 weekly downloads and 108 GitHub stars vs. prettier-plugin-package 1.4.0 which has 34,841 weekly downloads and 88 GitHub stars vs. sort-package-json 3.3.1 which has 2,506,349 weekly downloads and 863 GitHub ...
🌐
JetBrains
youtrack.jetbrains.com › issue › WEB-48071 › Prettier-and-prettier-plugins-doesnt-work-with-multiple-modules-project
Prettier and prettier plugins doesn't work with multiple ...
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
Top answer
1 of 1
1

OK, I've found a halfway decent way to integrate without hacking.

  1. First remove the general setting "auto-sort" from your settings.json to ensure that it does not apply globally to all files. Here's what you should remove:
"editor.codeActionsOnSave": {
    "source.sort.json": "always"
}
  1. Next, reapply the setting specifically for JSON files (set Prettier as the default formatter):
"[json]": {
  "editor.codeActionsOnSave": {
    "source.sort.json": "always"
  },
  "editor.defaultFormatter": "esbenp.prettier-vscode"
},
  1. Classify package.json as jsonc (JSON with comments), which is not targeted by the auto-sort setting. Add this to your settings.json:
"files.associations": {
  "package.json": "jsonc"
}
  1. Finally, set the default formatter for JSONC files, and deactivate auto-sort:
"[jsonc]": {
  "editor.codeActionsOnSave": {
    "source.sort.json": "never"
  },
  "editor.defaultFormatter": "esbenp.prettier-vscode"
},

Until a more convenient solution is implemented by the VSCode developers, you can use this solution here.

Every JSON will be sorted and every JSONC not. You can extend this list below:

"files.associations": {
  "package.json": "jsonc", 
  "settings.json": "json"
}

With this setup, package.json remains untouched by the auto-sort feature, while files like settings.json continue to be sorted. This distinction hinges on the file extension — json for sorting, jsonc for no sorting.

Be aware of the last letter c

Another Example

  "files.associations": {
    "de.json": "json",  // sort
    "en.json": "json",  // sort
    "fr.json": "json",  // sort
    "nx.json": "jsonc", // do not sort
    "package.json": "jsonc", // do not sort
    "project.json": "jsonc", // do not sort
    "settings.json": "json", // sort
    "tsconfig.app.json": "jsonc", // do not sort
    "tsconfig.base.json": "jsonc", // do not sort
    "tsconfig.e2e.json": "jsonc", // do not sort
    "tsconfig.editor.json": "jsonc", // do not sort
    "tsconfig.json": "jsonc", // do not sort
    "tsconfig.lib.json": "jsonc", // do not sort
    "tsconfig.spec.json": "jsonc" // do not sort
  },
🌐
Cloudsmith
cloudsmith.com › navigator › npm › @ianvs › prettier-plugin-sort-imports
@ianvs/prettier-plugin-sort-imports (4.7.0) - npm Package Quality | Cloudsmith Navigator
$npm install @ianvs/prettier-plugin-sort-imports · /Processing... ✓Done · Start your free trial · 4.7.0 · Stable version · 6months ago · Released · Loading Version Data · NPM on Cloudsmith · Learn more about NPM on Cloudsmith ·
🌐
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
🌐
npm
npmjs.com › package › prettier-plugin-json-sort
prettier-plugin-json-sort - npm
December 9, 2023 - A prettier plugin to sort package.json and tsconfig.json.. 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
🌐
Prettier
prettier.io › docs › configuration
Configuration File · Prettier
{ "overrides": [ { "files": ".prettierrc", "options": { "parser": "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
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 Trends
npmtrends.com › prettier-package-json-vs-sort-package-json
prettier-package-json vs sort-package-json | npm trends
Comparing trends for prettier-package-json 2.8.0 which has 43,898 weekly downloads and 155 GitHub stars vs. sort-package-json 3.4.0 which has 2,076,994 weekly downloads and 880 GitHub stars.