Sort your dependencies!

The Context

Dependency management is a crucial aspect of any project. In the Node.js world, npm is the standard tool for installing and managing dependencies, but let’s not forget its counterparts yarn and pnpm. The package.json file lists all the dependencies and their versions.

When working on a team project, it’s important to maintain the package.json file consistently. And that’s where the sort-package-json package comes in.

sort-package-json

It allows you to sort the package.json file alphabetically based on package names. This ensures consistency in the order of dependencies, making the file more readable and maintainable.

Illustration of a broom

In addition to dependencies, which are sorted by default if you’ve only installed them via command line (npm install XXX), sort-package-json also sorts other fields in the file, such as author, project name, version, etc.

For this purpose, the package utilizes predefined rules defined here.

Usage

Using it is simple! Just run npx sort-package-json and you’re done!

But you can also integrate it into a broader standardization strategy for your project, for example, by adding it to the tasks executed by your Git hooks using Husky, as described in the documentation here.