This post is about using modern Node.js modules.
Intro
Start typing in the search bar of any browser node modules and one of the
first three suggestions will be node modules meme.
Why developers of back-end languages, outside the Node.js environment, started to create
jokes about the node_modules
folder?
Is it heavy or misused?!
Problem
In the early days of Node.js which was created in 2009 and just after the release of the
package manager in 2010, anyone with a little knowledge of JavaScript could publish a
package.
Not only packages were massively created, but no one cares at the time if they were good or
not!
People using Node.js back then just searched for the package that will get the job done and
installed it.
Then tutorials about the most used modules came out and every newcomer took the path.
The problem was, and sadly still mostly is, that developers creating packages to resolve
common tasks search for existing modules to a particular aspect and add it as a dependency
of theirs to save time.
As a result, some of the most used packages are a bundle of a considerable amount of other
ones, leading to gasworks when it comes to creating an app using them and inevitably making
the node_modules
folder the heaviest object in the universe...
Solution
The most famous web framework for Node.js is indisputably
Express with over
28 million downloads per week!
Its npm page indicates that it relies on 31 direct dependencies while looking at its
graph we can see that its direct
dependencies and theirs expand to 58 with a minified bundle size of 573 kB!
So the majority of Node.js developers don't think twice to get a web framework and
install Express for 2 main reasons:
- They are in their comfort zone with it
- They are just being as lazy as they can
Aren't there in the vast Node.js ecosystem any decent alternatives?
Well, here are two in the following table:
Package | Dependencies | Minified Bundle Size |
---|---|---|
itty-router | 0 | 4kB |
hono | 0 | 20kB |
Hono was not designed for Node.js in the first place but it has an adapter of 3kB with no dependency.
Conclusion
Some of you may already think that an application is not only about a web framework.
My simple answer is that I've updated
Blog-Doc:
The simplest Node.js CMS and SSG!
to use 7 dependencies only instead of 145 and you can read more about it in
From 145 to 7 💪.