Node.js dependencies weigh too much

dependency-managementnode.js

Recently I started playing with node.js.

Now, every node tutorial out there states that you should start with

npm init

and then, say you want some standard server framework, say you choose express:

npm install express

but then you'll want many more things you are used to from worlds like ASP.NET.

I talk about template engines (jade) and stylesheet pre-processors (SASS).

And then they tell you "install gulp/grunt! so you can minify and uglify and run the server and so many other things automatically!"

And that means installing gulp, node-sass, and gulp-sass, and gulp-uglify, and maybe some more really cool stuff (tsd or babel, markdown etc)…

But all of those are heavy on your disk and project. Don't look for a moment and you can easily find yourself with 100MB+ disk size for that project (which hasn't even started yet!) not to mention 10000+ files since every node module brings its own dependencies, no matter that the same dependency is used by another module. And this is a very hard thing to move anywhere, let alone a web server.

Am I missing something? I don't think it's possible that so much praise is given to the node environment while such a clear flaw exists. Do I expect too much (after all I did try to use many tools at once), is there something trivial known to Node veterans to bypass this?

Best Answer

The recent left-pad issue is a prime example of the problem with this tendency in Node. When you depend on too many things, all of them are prone to go ka-pow, make your project harder to debug and, for a newcomer, harder to grasp the workings of the language.

Now good Node.js programmers know to write minimalistic applications, where dependencies are concerned. The less things you depend on - the better. Need to pad strings to the left? Code it in a helper, it's 11 lines of code with the blanks. Need to number your string rows? Code it in, it's less than 100 lines of code.

Even for more complicated tasks, like project management, I'd suggest to stick to Makefiles while your project is simple enough - grunt and gulp are really, really useful for giant projects that have a lot of heavy-lifting to do. But for your SPA blog? Write a Makefile, it takes 5 minutes and you know how it works.

The temptation to just browse npm every time you need to write 3 lines of code is great, but should be resisted, whenever it's reasonable. Don't include jQuery if you have 3 DOM manipulations, don't use angular for that static promo page, do not use express for a simplistic server. But you're coding a CMS? You'd have to be crazy not to use packages like jQuery, underscore and what's not. Working with 10 collection types, 3 dbs and querying them all the time? You'd be insane not to use underscore and a few others. Just think 'do I save enough time by installing this package?' or 'Can't I just code this in for half an hour or so?'

Related Topic