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

Statement Functions

At a small suggestion in #python, I wrote up a simple module that allows the use of many python statements in places requiring statements. This post serves as the announcement and documentation. You can find the release here . The pattern is the statement's keyword appended with a single underscore, so the first, of course, is print_. The example writes 'some+text' to an IOString for a URL query string. This mostly follows what it seems the print function will be in py3k. print_("some", "text", outfile=query_iostring, sep="+", end="") An obvious second choice was to wrap if statements. They take a condition value, and expect a truth value or callback an an optional else value or callback. Values and callbacks are named if_true, cb_true, if_false, and cb_false. if_(raw_input("Continue?")=="Y", cb_true=play_game, cb_false=quit) Of course, often your else might be an error case, so raising an exception could be useful

How To Teach Software Development

How To Teach Software Development Introduction Developers Quality Control Motivation Execution Businesses Students Schools Education is broken. Education about software development is even more broken. It is a sad observation of the industry from my eyes. I come to see good developers from what should be great educations as survivors, more than anything. Do they get a headstart from their education or do they overcome it? This is the first part in a series on software education. I want to open a discussion here. Please comment if you have thoughts. Blog about it, yourself. Write about how you disagree with me. Write more if you don't. We have a troubled industry. We care enough to do something about it. We hark on the bad developers the way people used to point at freak shows, but we only hurt ourselves but not improving the situation. We have to deal with their bad code. We are the twenty percent and we can't talk to the eighty percent, by definition, so we need to impro