logo

How Many Dependencies Does Your Project Really Have?

Mikołaj Sykuła
Mikołaj Sykuła
github icongithub icon
Oct 8, 2023
How Many Dependencies Does Your Project Really Have?

Introduction: The Light-Hearted Side of "node_modules"

Ah, node_modules – every JavaScript developer's favorite folder (not really, but let's pretend). It's where tiny utility packages, like is-odd, find their place alongside larger, more comprehensive libraries. Who hasn't felt a mix of amusement and bewilderment on discovering packages that accomplish tasks which seem... trivial?

Speaking of amusing, remember that security advisory which cautioned against a particular npm install? If you thought that was rare, think again!

Is It As Bad As They Say?

Spoiler alert: It might be even worse.

You might argue, "It's just a tiny package, what harm can it do?" That's where the actual cascade begins. Every package you add brings along its own plethora of dependencies, and those dependencies have their own dependencies, and so on. It's like the matryoshka dolls of the JavaScript world.

To provide a bit of perspective, most small random projects can have over a whopping 1000 dependencies. Yep, you read that right.

Diving Into Your Dependency Tree

The real eye-opener is when you delve into the intricacies of your dependency tree. Just a simple audit can reveal the magnitude of what your seemingly innocent package.json file drags into your project.

Tool I used:

My project written in Next.js

Package.json

json
Loading...

Audit

My Next.js project have 652 dependencies, as you see in package.json, project is not that big, but still we have buch of dependencies, but wait this is nothing.

CSV
Loading...

Tree

Next.js project dependencies tree

TreeMap

Nest.js project dependencies treemap

Example Nest.js backend project

Package.json

json
Loading...

Audit

My NestJS project have 719 dependencies, after i saw the audit of next.js, nest.js didn't suprice me at all.

CSV
Loading...

Tree

NestJS project dependencies tree

TreeMap

NestJS project dependencies treemap

Example of a simple React Native project

Package.json

json
Loading...

Audit

My React Native project have 1434 dependencies, that suprised me a lot. I've just created this project, and work on it maybe for a week.

CSV
Loading...

Tree

You don't see a image, probably it's still loading, it have 6MB 😂

React Native dependencies tree

TreeMap

React Native dependencies treemap

To Contribute or Not to Contribute

The Case for Creating Your Own Packages

Before you add another random package for a function that's probably ten lines of code, pause and reflect. Do you really need it? More often than not, you'll realize that writing that function yourself or simply copying what you need will suffice.

Unraveling the Nested Dependencies

However, if you find that you're relying on a package that itself has numerous unnecessary dependencies, consider contributing. Trim down the fat, so to speak. If you've got the time and expertise, pitch in and help the community streamline things. Your future self (and your fellow devs) will thank you.

The Message: Think Before You "npm install"

Every time you're on the brink of adding another package, think about the cascade it might bring along. Every additional dependency is potential technical debt, not to mention the security implications.

Moreover, if you're in the privileged position of having time on your hands, consider contributing to existing packages or even creating your own streamlined versions. The npm ecosystem thrives because of contributors, and you could be the next one to make it better!

Conclusion

Dependencies are both a boon and a bane. They save time and provide functionality but also bloat projects and introduce risks. Striking a balance is key. Use what you need, contribute when you can, and always be judicious with your npm install commands. Remember, with great power (to install) comes great responsibility!