Skip to main content

Javascript Module Loaders Considered Harmful


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.


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.


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…
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

CARDIAC: The Cardboard Computer

I am just so excited about this. CARDIAC. The Cardboard Computer. How cool is that? This piece of history is amazing and better than that: it is extremely accessible. This fantastic design was built in 1969 by David Hagelbarger at Bell Labs to explain what computers were to those who would otherwise have no exposure to them. Miraculously, the CARDIAC (CARDboard Interactive Aid to Computation) was able to actually function as a slow and rudimentary computer.  One of the most fascinating aspects of this gem is that at the time of its publication the scope it was able to demonstrate was actually useful in explaining what a computer was. Could you imagine trying to explain computers today with anything close to the CARDIAC? It had 100 memory locations and only ten instructions. The memory held signed 3-digit numbers (-999 through 999) and instructions could be encoded such that the first digit was the instruction and the second two digits were the address of memory to operate on

The Range of Content on Planet Python

I've gotten a number of requests lately to contribute only Python related material to the Planet Python feeds and to be honest these requests have both surprised and insulted me, but they've continued. I am pretty sure they've come from a very small number of people, but they have become consistent. This is probably because of my current habit of writing about NaNoWriMo every day and those who aren't interested not looking forward to having the rest of the month reading about my novel. Planet Python will be getting a feed of only relevant posts in the future, but I'm going to be honest: I am kind of upset about it. I don't care if anyone thinks it is unreasonable of me to be upset about it, because the truth is Planet Python means something to me. It was probably the first thing I did that I considered "being part of the community" when I submitted my meager RSS feed to be added some seven years ago. My blog and my name on the list of authors at Plan

Pythonic Defined

Introduction Losing is Good Strings Dictionaries Conclusion Introduction Veterans and novices alike of Python will hear the term "pythonic" thrown around, and even a number of the veterans don't know what it means. There are times I do not know what it means, but that doesn't mean I can define a pretty good idea of what "pythonic" really means. Now, it has been defined at times as being whatever the BDFL decides, but we'll pull that out of the picture. I want to talk about what the word means for us today, and how it applied to what we do in the real world. Languages have their strengths and their idioms (ways of doing things), and when you exploit those you embrace the heart of that language. You can often tell when a programmer writing in one language is actually more comfortable with another, because the code they right is telltale of the other language. Java developers are notorious for writing Java in every language they get their hands on. Ho