Skip to main content

Javascript Module Loaders Considered Harmful

Introduction


I’m coming to an opinion of Javascript module loaders that is profoundly negative and I’d like to express why I think they are, generally, a bad idea. However, I do think they have a place, which I’ll get to at the end.

Now, I understand I might be in the minority here. Between the competing specifications of CommonJS and AMD modules, loader systems like RequireJS or the (honestly really awesome) Google Module Server, and the huge cultural influence of Node on the Javascript world, you’d be hard pressed to argue against Javascript modules these days. Scripts are old hat, too stupid, too inflexible. Everyone knows that and no one would make an argument in their favor, right?

I’m going to step out on a limb and say “Javascript Module Loaders Considered Harmful” and I know the baggage involved with declaring something “Considered Harmful”. I mean every ounce of context that phrase carries with it, and I hope I can persuade you.

Harm #1: Confused Debuggers


Module loaders often make debugging difficult in a number of obvious and subtle ways.

Compression, transpiling, and other pre-processing in the pipelines that typically support module loaders (which are not always necessary, but are in use in tandem consistently) rewrite scripts from the input module to the source that actually gets parsed by the browser. There is a frequent mismatch when debugging in the browser between your actual source and the code you see in the debugger. Source Maps are helping this, but support for them in both browsers and tool chains is still underwhelming.

Often times, even when the source is unchanged or the source maps work properly, the simple nature of the dynamic tags used by module loaders causes issue. Debuggers might not recognize the same script between page loads, without an inline <script> node, causing breakpoints to be lost, for example.

It is very hard to justify module loaders on their benefit when they can make debugging your code, the very thing they aim to help you organize and simplify, so much more difficult.

Harm #2: Module Load Order


Perhaps the single most common and frustrating thing to deal with in module loaders is the order of load or script execution. Compared to the expected load order of a series of <script> tags in your page, understanding when each module will be executed can be an exercise in frustration quickly.

Execution order from module loaders is usually a factor of the race to which modules are downloaded first and the inter-module dependencies defined which will keep actual execution of a downloaded script pending until its dependencies have all been loaded.

There is obvious merit here and helping us to define and understand dependencies, and to provide clear references between them in the form of things like require() calls, is certainly admirable.

However, the fact is that the vast majority of our projects don’t have such complicated intra-module dependencies to really justify this.

Harm #3: Workflow Complication


Simply put, using a module loader instead of simple scripts is simply harder to work with.

There are well known difficulties integrating other libraries that don’t use the same module system. That there are multiple such systems makes this worse. Library authors are forced to provide extra wrappers for all the different loaders or pick a side when their users have or care not to.

When you bring in new developers to a project or when a developer finds a library she wants to use which has decided to distribute itself as a module in a specific system, like RequireJS, there is an added barrier to use with little apparent benefit to a developer who just expected another <script> tag to their existing project. Is it worth integrating someone else’s preferred loader to use their simple library? I fear this may lead to fragmentation of our already disperse ecosystem.

Conclusion


The harms are not justified compared to the benefit when the majority of web applications utilize such a small and constant set of Javascript.

There is a case for module loaders, which can only really be made when the costs are outweighed by the benefits, and this is only really verified when the costs of not using a module loader are greater than the costs when you do use a module loader. Costs of not using a module loader are incurred when you have a large enough body of code that loading all of it at the start of your app is detrimental to performance, user experience, or both.

Comments

RichB said…
It's useful to separate the notion of module loaders from the notion of modules.

Clearly defining inter-module relationships and dependencies is fantastically useful.
Browser-driven asynchronous module loading is IMHO less useful.

Your article touches on this when you say:

> There is obvious merit here and helping us to define and understand
> dependencies, and to provide clear references between them in the form of things
> like require() calls, is certainly admirable.

The right solution is to use an AMD shim (AMD-Lite) module system to declare and resolve dependencies - but to use server-side bundling/minification of JavaScript.

When you do that, and when you use the AMD shim to only load your own script (not 3rd-party libraries) then the benefits are great.
What's more, you can then use the browser's built-in asynchronous script loading if you so choose. Simply set the async attribute on the script tag. Document ready happens sooner and the factory functions will only get executed when all dependencies are satisfied.

And for some AMD shims, you can ask it if there are any unresolved dependencies. If you are lucky, you will get a hierarchical tree of dependencies printed to the console.
ionelmc said…
I think this is common in systems that use inversion of control. Because that's what the module systems are - god damn IOC assemblers.
Anonymous said…
Perhaps you meant to say "JavaScript Module Loaders Considered Harmful for Small Projects".

I can't imagine you would make the same argument if you were working in a JavaScript application with 100+ JS files.
John Zabroski said…
When you say, "the fact is the vast majority of our projects don't have such complicated intramodule dependencies to really justify this", I wish you put detail sentences. This sentence should be the introduction to a paragraph or section.

For example, what happens if a client makes a request that requires validation between two modules? ALSO, what are your modules? For us, a module may be a filter, which in turn is a part of a filter list renderer module for searching.

Without these tinier modules you limit faceted compositionof your UI. Also, you run into ppotential for other kinds of race conditions related to two librarians using the same names for things and the order of your script execution.

That all being said, there is one gross thing about (current) AMD module LOADERS and it is that they can't, to my knowledge, statically detect infinitely recursive module references.

Anyway, thanks for trying. You at least made me think a bit, even though I think you are a bit naive and trying to enforce your naive worldview on others, I still greatly appreciate the effort and courage to speak your mind.

Please try to fill out your ideas with real world examples and explanations of how you divide your code into modules.
WishCow said…
This comment has been removed by the author.
Norbert Kéri said…
Re: http://ihaveabackup.net/2014/02/16/javascript-module-loaders-considered-harmful/
Nicolas Joyard said…
Hmm, this just seems like FUD nonsense to me.

#1 confused debuggers

You don't have to debug using compressed/preprocessed sources. I have never done so. Source maps do work very well (I have never had a problem with them so far), but I have yet to see a bug that cannot be reproduced using the original sources.

You wouldn't debug a C program without debugging symbols, would you ?

#2 Load order

Relying on load order is just bad practice, period. It is even worse when working with loader(s) that rely on module dependencies.

Even when working with plain script tags, relying on load order is just opening the doors to maintenance problems.

#3 Workflow complication

This is a non-problem IMO. Each module standard has its ecosystem with corresponding libraries. Most loaders provide a way to shim around other module standards, that you only have to setup once for your first project.

In the end, most tools do complicate workflow. Bugtrackers do. Compilers do. VCS do.

But they also solve a bunch of problems for you. And so do module loaders. I have never been so happy with maintaining my JS projects since they all use a module loader. I don't have to worry about manual dependency resolving (load order anyone ?...), about minification...


As a conclusion, working with loaders is a different workflow, that is for sure. But saying it is beneficial or harmful is nonsense to me. It is very beneficial in a lot of situations and projects. It MAY be harmful in some specific situations, but that's about it.

In the end, a tool is made to do what it has been made to do. You'll always have problems if you try to nail down staples with a screwdriver. But screwdrivers are awesome at driving screws.

Popular posts from this blog

On Pruning Your Passions

We live in a hobby-rich world. There is no shortage of pastimes to grow a passion for. There is a shortage of one thing: time to indulge those passions. If you're someone who pours your heart into that one thing that makes your life worthwhile, that's a great deal. But, what if you've got no shortage of interests that draw your attention and you realize you will never have the time for all of them?

If I look at all the things I'd love to do with my life as a rose bush I'm tending, I realize that careful pruning is essential for the best outcome. This is a hard lesson to learn, because it can mean cutting beautiful flowers and watching the petals fall to the ground to wither. It has to be done.

I have a full time job that takes a lot of my mental energy. I have a wife and a son and family time is very important in my house. I try to read more, and I want to keep up with new developments in my career, and I'm trying to make time for simple, intentional relaxing t…

The Insidiousness of The Slow Solution

In software development, slow solutions can be worse than no progress at all. I'll even say its usually worse and if you find yourself making slow progress on a problem, consider stopping while you're a head.

Its easy to see why fast progress is better: either you solve the problem or you prove a proposed solution wrong and find a better one. Even a total standstill in pushing forward on a task or a bug or a request can force you to seek out new information or a second opinion.

Slow solutions, on the other hand, is kind of sneaky. Its insidious. Slow solution is related the Sunk Cost Fallacy, but maybe worse. Slow solutions have you constantly dripping more of your time, energy, and hope into a path that's still unproven, constantly digging a hole. Slow solutions are deceptive, because they still do offer real progress. It is hard to justify abandoning it or trying another route, because it is "working", technically.

We tend to romanticize the late night hacking…

Finding "One Game A Month"

I was really excited about the One Game A Month challenge as soon as I heard about it.
For about two years I've struggled in fits and starts to make my way into game development. This hasn't been productive in any of the ways I hoped when I started. Its really difficult to be fairly experienced as a developer, which I believe I am in my day job as a web developer, while struggling really hard at an area in which your experience just doesn't exist.
Its like being a pilot who doesn't know how to drive.

But this challenge provided a new breath to this little hobby of mine. It gave me a scaffolding to experiment, to learn, to reflect on finished projects. I had spent far too much time on game projects that stretched on far past their exciting phases, bogged down by bad decisions and regret.
And it has worked.
I have a lot to learn. I have a lot of experience to gain through trial and error and mistake and discovery. I have a lot of fun to be had making more small games t…