How to sort package.json automatically

Published on in JavaScript

Last updated on

Just run npx sort-package-json.

This takes about 4–8 seconds on my machine because npx has to first download the sort-package-json package. If that's too slow for you, install the package globally:

#  i =   install
# -g = --global
npm i -g sort-package-json

Then you can run just:

sort-package-json

And it will take less than a second.

You can also install the package locally and even run it in Git's pre-commit hook (using Husky or Lefthook) or in your CI pipeline. More options are mentioned in sort-package-json's documentation.

Source: My own Stack Overflow answer