I've been working on lots of old npm packages that have their dependencies all out of order. They're shrinkwrapped packages, so updating dependencies is a bit of work (testing and verifying that the dependency changes didn't break anything), but I'm manually moving some dependencies from the devDependencies
key to the dependencies
key, and I don't want to do anything except alphabetize them before I commit. Rather than doing it manually, is there an easy way to programmatically alphabetize them with npm?
-
4Could you just use your text editor to sort the dependency lines? Sublime Text has built-in support (Edit -> Sort Lines) and IntelliJ has a plugin: plugins.jetbrains.com/plugin/5919 – erwaman Dec 7 '16 at 3:53
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.
-
27you can simply run
npx sort-package-json
if you are usingnpm>=5.2.0
– diachedelic Nov 6 '18 at 3:45
Just run npm remove --save anything
or npm remove --save-dev whatever
and npm will sort that section, without actually touching any of the content. Of course you should make sure that the package name you pass it (which can be anything, spam your keyboard) isn't in your package.json.
-
4
-
14
-
3I love this developer way of doing things without having to rely on some external tools )) – haynar Oct 31 '18 at 8:36
-
2Even shorter:
npm r -S example; npm r -D example
(r
,rm
andun
are the shortest default aliases ofnpm uninstall
) – Matias Kinnunen Apr 22 '20 at 18:09 -
In addition to martias answer, you can just run:
npx sort-package-json
This won't install it permanently. You need npm >5.2.
-
3
-
5This package will also sort the
"scripts"
section of the package.json. Which may not be desirable for some. – Barry Jones Apr 2 '20 at 5:41
I have found the npm-sort
package, which seems to work quite well, with the minor niggle that it removes the trailing newline from the package.json
file.
You might also want to take a look at fixpack, a CLI to update your package.json following their (slightly) opinionated order. You can however add a .fixpackrc
to define your own rules, the defaults are:
- 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
in VS-code there's a good packagesorter ofr the whole json file..
there's also something out there called "sortier" which sorts more, and is awesome.