Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Can you help me understand the benefit of require.js?

I'm having trouble understanding the benefit of require.js. Can you help me out? I imagine other developers have a similar interest.

From Require.js - Why AMD:

The AMD format comes from wanting a module format that was better than today's "write a bunch of script tags with implicit dependencies that you have to manually order"

I don't quite understand why this methodology is so bad. The difficult part is that you have to manually order dependencies. But the benefit is that you don't have an additional layer of abstraction.

Here's my current JS development work flow.


When in development-mode, all scripts have their own tag in the DOM.

<script src="depA1/dep1-for-module-A.js"></script>
<script src="dep2-for-module-A.js"></script>
<script src="moduleA/moduleA.js"></script>
<script src="dep1-for-module-B.js"></script>
<script src="module-B.js"></script>
<script src="moduleC/module-C.js"></script>
<script src="script.js"></script>

There is no abstraction layer. This allows me to better debug individual files. The browser reads separate files, so I can debug with Developer Tools. I like how it's straight-forward.

Dependencies are basically managed right here. depA1 needs to be listed before moduleA. It's explicit.


Modules are 'transported' by attaching to the global namespace.

( function( global ) {
  var dep1 = global.depA1;
  var dep2 = global.depA2;
  function ModuleA() {
    // ...
  // export
  global.ModuleA = ModuleA;
})( this );


All scripts are concatenated and minified. One HTTP request on load.

<script src="site-scripts.js"></script>

The Concat + minify task is maintained separately. It's part of a build process. Makefile or what-have-you. For dependency management, the ordering of scripts matches how they were listed in the HTML.

Changing modes

This can be done easily with some sort of configuration and templating. For example, by setting prod_env config variable to true or false, the site is either in production, serving the one file, or development mode, serving every single file.

{% if prod_env %}
  <script src="site-scripts.js"></script>
{% else %}
  <script src="dep1/dep1-for-module-A.js"></script>
  <script src="dep2-for-module-A.js"></script>
  <script src="moduleA/moduleA.js"></script>
{% endif %}


  • What benefit does require.js provide over this workflow?
  • How does require.js address minimizing HTTP requests? Is this any better than concat/minifing all the scripts?

cowboy commented Jan 31, 2013

Pretend you're not a web developer that's used to writing a dozen script tags into a webpage for a moment.

If you didn't need to know that module awesome-thing required one or more other modules, and just specifying awesome-thing actually worked, would you be happy just specifying awesome-thing or would you want to be aware of and specify all of its dependencies, their dependencies, etc?

Adding to what @cowboy said, the lots-of-script-tags, manage-my-own-dependencies approach might work for a small project, but it rapidly falls apart for anything sizeable. It's entirely plausible that a rich client-side JS app will have thousands or even tens of thousands of lines of code (or more!), and managing a codebase of that size requires small modules, one to a file.

With RequireJS, I can have as many small files as I want, and don't have to worry about keeping track of dependencies or load order -- things Just Work. The RequireJS optimization tool also allows me to make builds that go beyond the standard concat-and-minify technique: I can create built files that only load certain parts of an app at startup, and then separate built files for other parts of the app that can be loaded on demand. With the optimizer, you not only end up with a small number of HTTP requests, but you also get the ability to manage the size and shape of the JS payload.

Finally, RequireJS makes it possible to write an entire, modular app without touching window. I don't have to worry about naming conflicts, and I can choose the best name for a thing at the time I need that thing, rather than being stuck with a single, global name. Smart people will argue as to the importance of this, but I find it pleasant, and moreso when I'm working on a team.

Bottom line: having used RequireJS (and the Dojo module system before that), I have a hard time imagining developing anything even slightly non-trivial without it. It just makes all sorts of things way too easy.

To add to what @rmurphey said, "modules" in the app sense may or may not include something more than just JavaScript dependencies. AMD plugins allow me to package other types of resources with my module. For example, my typical AMD controller module may look like this:

], function(can, view) {
  return can.Control("Tooltip", {
    render: function() {

In some other file…

require(["controllers/tooltip"], function( Tooltip ) {
  var tooltip = new Tooltip("#id");

I can be confident that when I require(['controllers/tooltip'], fn…), that it's separate view file and associated stylesheet have also been loaded. This is a great help, not only to keep your components/controllers modular, but it lends a hand when unit testing as well.

For example, let's say my unit tests depend on the tooltip component being hidden via display: none; when being initially rendered, and this style is defined in stylesheets/tooltip.css. If I simply dropped in the script tag, and the css was missing, my tests would break. With AMD, I can simply require the module, and know that all of it's deps (including the css) come along with it.

What a coincidence--I'm just this moment putting together slides about AMD and Require.js!

Dependencies are basically managed right here. depA1 needs to be listed before moduleA. It's explicit.

It's only explicit insofar as the example file names describe their dependencies. Without that, the dependency graph is flattened to a list. Complex dependencies cannot be expressed sequentially.

For dependency management, the ordering of scripts matches how they were listed in the HTML.

Require.js helps you to avoid duplicating this information across Makefiles and HTML templates.

As for minimizing requests, see @rmurphey's comments on the Require.js optimizer.

Just to add to what @murphey and @cowboy have said, the real value of AMD is not so much the "asynchronous" part as it is the "module definition" part. When dealing with large-scale projects we have to think about the experience of other developers who will be working on that code, now or sometime in the future. There are a few main wins:

It's takes so much less time for another developer to debug or change a module if they know exactly where that module's file exists in the project. Carefully ordered script tags will tell you WHAT modules are being loaded, but it won't tell you explicitly where that module is being used. With AMD the path to the module (or a string that points to the path to the module) is strictly defined at the top of the file. Developers can quickly trace functionality without having to step through a debugger.

Secondly, because the coupling points between modules are so strongly defined by the require calls it is incredibly easy to change out or modify a dependency. The example I like to use is updating to a new version of vendor code such as jQuery. With AMD, you can simply change what module is being required; the variable name used in the closure remains the same. Changing the version globally means you have to perform regression tests across the entire site. Changing it at one or a few modules at a time means the QA process can be much more incremental. Not that I would recommend running two versions of jQuery in production, but you can see the point that you have so much more control over when and how changes occur in your codebase.

Lastly, AMD generally allows us to write more cohesive code. As @rmurphey stated above, you can write smaller, more concise modules and not have to worry about keeping track of them all. This kind of abstraction really helps me identify when I'm writing the same code over and over again as well, so it's made my modules much more utilitarian and I've reduced the number of modules being loaded overall.

So, in short, AMD really provides a framework to writing code that is going to be more accessible to the next developer who comes along and touches it. You'll especially appreciate it when that developer is you six months from now and you haven't touched the code in that long.

What about single-page webapps? When you have a lot of components and you have to load them dynamically it helps a lot. You just need the path of the module to load it and you don't have to deal with its dependencies (and it's a waste of bandwidth to include all the module dependencies in it's file, while you may have already loaded them).

It's also good when you just need certain components of a medium/big size framework. You can download the whole framework and just load the components you need. You don't have to load everything that comes with the framework and you don't have to find out what are the dependencies of each component you are using. In a project I developed, for example, I needed to use the touch events module of the jQuery mobile framework (just this part). I just had to download the whole framework (all the components are AMD modules) and add 'jquery-mobile/events/touch' as a dependency of one of my modules. The events module has jquery, and as dependencies and I didn't have to know it to use the module I needed. It helped a lot to me in this case.

As someone relatively new to using requirejs, who was also skeptical about the benefits that it would bring, the biggest win has been being able to see what external modules are being used just by looking at the top of the file. It's allowed us to break our new projects down into many small pieces, and still figure out very quickly what the dependencies are of a specific piece.

Just a quick example from an action menu component:

  FinancialTransaction = require 'cs!../models/financial_transaction'
  FinancialTransactionsHelpers = require 'cs!../financial_transaction_helpers'
  FinancialTransactionsActionsTemplate = require 'text!../templates/financial_transaction_actions.html'
  MoveTransactionModal = require 'cs!./move_transaction_modal'
  UndoDelete = require 'cs!./undo_delete'

Many of these arguments aren't really for RequireJS or AMD. They are for modular components and better dependency management. I would argue that you can do this much more easily in more traditional, synchronous CommonJS frameworks that do not have AMD's asynchronous, callback hell or overhead:

@jonathanong agree, really this is about having a tool for modularization -- for me, RequireJS fits that bill, but I'd rather see a developer pick some module/dependency management system -- AMD, CommonJS, heck, even the Rails asset pipeline -- than see them piling script tags into index.html. It seems that's the question being asked here -- "what's the matter with a bunch of script tags, anyway?" -- and the comments seem to agree that the answer is "plenty" :)

bendman commented Jan 31, 2013

@jonathanong Agreed. CommonJS is great for projects which I can control more of the process, and generally seems cleaner than the callbacks in AMD handling. That being said, using require.js (or building your own simple AMD handler) is the approach I take when working on a project with a well established code base when I need a good modular structure without the overhead of build steps, don't have the ability to run Node, or want to be able to run a modular site without a server at all.

The asynchronous capabilities of AMD can be used for other things if written correctly. Core functions within an AMD handler can be used for state management, as well as pre-caching of other resources (images, css, JSON - although I personally only use it for JSON). These can be done other ways, but it's nice to have a single global "async" set of utility functions for a site.

Additionnally to already explained dependency management benefits, when you say "Modules are 'transported' by attaching to the global namespace", I read "Modules are polluting global namespace". Global pollution is bad, dangerous and easy to avoid with AMD.

draffauf commented Feb 1, 2013

Beautiful! I was wondering about this exact thing when I built my first simple contacts module and app wrapper. I'll have to dig into this more.

@ralphholzmann I wouldve thought you'd be using StealJS for your dependency management, i have been using it for the past 2 years but im slowly starting to look at RequireJS for its AMD and for the smaller footprint it has on a project.

in the requirejs build process, is it possible to concat / minify every CSS like StealJS does with its production.css file? Same for templates?

I also took a look at Almond for projects not needing on the fly requires.

Your feedback / comments would be appreciated ;)

Great discussion by the way, ill keep checking it!

danro commented Feb 1, 2013

As far as RequireJS vs other module solutions, I have yet to see a library offer such a seamless / flexible way to work with JS modules in front-end dev. It does not add a compile step to each save, or a watch script to wait for. The only thing it asks is that you add a define() wrapper to your modules, and optimize before deployment.

Save & reload.

dubbs commented Feb 1, 2013

@falzhobel It's recommended that you have a single main.css file that uses @import to reference your css files, see

I personally recommend sass/compass, and running 'compass watch'. You can set it up so you have a main.sass file that imports your other sass files. This way, every time you save, your css will be compiled into a single file.


desandro commented Feb 1, 2013

Thank you for all the wonderful responses. Wow, this is a helpful community.

Where this is all going, for me, is thinking about package management. I'm now understanding why Component is the way it is, integrating this sort of module system.

If I may, I'd like to take veer discussion down a related path...

What's blowing my mind:

Nested dependencies

I publish a lil' library like Vanilla Masonry. Right now, this is doing the global namespace pollution thing, but for the sake of the example, let's say it's implements define() properly

define(function() {
  function Masonry() {...}
  return Masonry;

Now other developers can implement the Masonry script via require.js. YAY.

Okay... so what happens if I want to define a dependency for Masonry, like Underscore?

define( [ 'underscore' ], function( _ ) {
  function Masonry() {/*...*/}
  _.extend( Masonry.prototype, /*...*/ );
  return Masonry;

In order to define the dependency, I need to know the file structure. But, that's up to the developer, not to the library author.

How does Require JS handle this?
Can the Masonry library be implemented without the implementor having to change the source?
Or have we crossed over into the Treacherous Valley of Package Management?

keekerdc commented Feb 1, 2013

I would urge @desandro to peruse the top half of this post, which is a pretty good rundown of some things coming down the pipe in ES6; particularly the additional stuff linked in the section titled 'Where do these modules fit in with AMD?' If that wasn't enough of a spoiler, some consensus is gathering around trying to make the notion of modularity native, because many people do indeed find use out of it, that it helps them better organize their code.

To be fair, I think that the process and rationale outlined in the gist is perfectly rational; there's minimal ceremony and assumes a developer is smart enough to determine and express a proper order for a simple application. I don't think the point at which things get unmanageable approaches as rapidly as other commenters have suggested, even though that point surely does exist for exceptionally large apps. Regardless, if it's possible to load dependencies in a proper order through AMD, it's also possible through the maintenance of a flat build order, too.

But I also find myself nearly always authoring UIs atop Rails these days anyway, so I get the pipeline for free, which coincidentally does exactly what @desandro outlines between environments. I'm also writing in coffeescript and test-driving as much as possible, which if you're into, makes the concern of waiting on a watcher a bit moot, because you're probably going to resort to one anyway to run your tests, or compile your assets if you're outside Rails.

I say this knowing my situation and subsequent approach isn't universal, and I think that's just fine, and important to this discussion, because the vibe I got from this gist is that the author was feeling as though he was missing out on something obviously important that maybe he was doing wrong. AMD and libraries that operate on the pattern are simply tools that should be picked up and used if their rationale makes sense and solves a problem for you and your team. If that's not the case...

Since a lot of people already explained why modules are a good thing, I will reply the last @desandro comment

You don't need to know the file structure to consume the dependencies, I rarely put my dependencies on the top-level folder (baseUrl in RequireJS terms). The good thing about using plain strings to reference the dependencies is that they can point to anything you want, there are options like map and paths that can be used to change the module ID resolution logic, you can use it for instance to load lodash instead of underscore.js or to make some paths shorter:

  map : {
   '*' : {
     'underscore' : 'vendor/lodash' // all modules will load lodash instead of underscore
  paths : {
    'mout' : 'very/long/path/to/mout' // avoid typing long path all the time

// magic.js
define(['underscore', 'mout/string/slugify'], function(_, slugify){
  // will load "vendor/lodash.js" and "very/long/path/to/mout/string/slugify.js"

Nowadays there is also the shim config to handle legacy code but I usually wrap everything into define() to make it easier. The umdjs repository contains some examples of how to export libs to multiple environments (AMD, node.js, CJS, browser globals), in most cases you can solve it with just a few extra lines of code at the bottom of the lib:

if (typeof define === 'function' && define.amd) {
  define(Masonry); // AMD
} else if (typeof module !== 'undefined' && module.exports) {
  module.exports = Masonry; // node.js
} else {  
  window.Masonry = Masonry; // browser global

AMD is good because it's flexible, but it's also more complex than node.js modules since it gives more options. I switched all my projects to AMD ~2yrs ago and I'm not looking back... no.more.awkward.namespacing, no globals, no concat issues, lots of flexibility... Some of my projects have more than 500 source files, can't see myself doing the same thing manually.. - very happy RequireJS user here.

The ability to load templates and compile them automatically during build (without changing your code) is also great - plugins are an overlooked feature and one of the biggest strengths.

PS: There are tools to convert between the different module formats (see: r.js and nodefy) the authoring format is just a detail... pick whatever works for you. (I'm biased towards AMD :P)

@desandro yup. that's why i'm all for component. it's a synchronous require.js + bower package downloader. much lower overhead, less nesting, and you don't have to worry about all the things @millermedeiros just mentioned. only difference is you would just do a var _ = require('underscore'); var slugify = require('./mout/string/slugify'); instead (assuming that's what he's doing. I'm not sure what he's doing).

only downside i can think of is the lack of a built-in, automatic build process, but that's mostly because node.js' current watch command sucks.

if you remember my post about vanilla-masonry and component, the standalone version will have AMD support automatically.

tbranyen commented Feb 1, 2013

Hey @desandro to answer your question, I'd venture to say that all modules should require the most vague abstract name. Example: define(["underscore"] over define(["vendor/documentcloud/underscore"]. So long as everyone remains consistent (which they have been for package management reasons), you can easily map to the correct location in your own configuration.

One problem I have with RequireJS right now is the load path for nested dependencies that want to load modules relative to their location rather than the baseUrl. Maybe an embedded map configuration could work here, I am unsure.

tbranyen commented Feb 1, 2013

Also another added benefit is that you can remap dependencies, this makes it so that you only use jQuery or lodash even if other packages are including underscore and zepto.

Example of that:

Defining the dependencies of your files is more than a requirement for large scale project. There simply is no other way you can accomplish maintainability and collaboration between teams.

However i beg to defer from calling everything a module and certainly dislike AMD and its' derivatives.

There are 3 reasons for that.

AMD Is Not Focused On Solving Dependencies

It is also a way of defining how your code will be structured by forcing you to wrap it in a huge anonymous function, called the factory:

define(id?, dependencies?, factory);

Some people like this style some people don't. The main reasoning being that you don't want to leak variables to the global namespace. I understand that but the AMD pattern is not the only way to avoid global namespace pollution.

AMD Creates Closures For Each File

Since the definition of AMD requires that you wrap your code in an anonymous function this results in creating one extra closure for every file in your codebase.

We all are knowledgeable folks here and know what this means or doesn't mean.

AMD creates verbosity that's not required on production

The declaration / requirement statements are there in production code.

Whatever you state that each file requires on the top of your module, will be there in the production code. If AMD was a purely dependency management solution this should not happen. It should plainly make sure that the files are concatenated in the right order based on their dependencies and get out of the way after that.

You are sending over the wire ~1-3% more bytes, from what your JS app size is. That is too much.

What else?

Google's Closure dependency system is the most elegant solution by far. It does what it sais it does, takes care of your dependencies, nothing more and it does it very very good.

It can be ripped of the closure tools and used independently on any kind of project. It's something i plan on doing when i find a free weekend.

It doesn't dictate how you write your code. You want namespaced hierarchy? Cool! You want AMD modules? Certainly! You want something in betwen? Can do... no restrictions

And closure compiler in SIMPLE_OPTIMIZATIONS will take care and remove all the dependency statements from your production code leaving it bare as it should be.

(as i finished writing this rant i figured why not post it in my blog too)

Hey all!

FWIW, I think the format discussions are largely moot. AMD vs CJSM vs Harmony vs CoffeeScript vs TypeScript vs whatever. I know @jrburke has had some success supporting Harmony modules, and curl.js already supports native CommonJS Modules in addition to AMD (and will add other transpile-to-js languages in the future).

My first goal to tackle for 2013: allow devs to write their code and integrate 3rd-party libs written in any transpile-to-js language and -- with minimal configuration -- it'll all just work.

For the short term, I still highly recommend AMD since it's got the best browser support; the widest availability of plugins, loaders, and build tools; and the most coding flexibility (by far).

@jonathanong and @bendman: The loader handles the asynchrony for you. If you're dealing with callback hell in your AMD code, you're likely not understanding how AMD's define works.

Great discussion!

-- John

@dubbs I get your point, but what about when using external plugins' CSS files? I don't want to convert them all to .scss and then have to import them manually in my main.scss.

What I currently do with StealJS in production is that I have a compiled production.css (all my js-related styles) which I then concat within my styles.css (actual site CSS)

I'm looking for a similar approach but with RequireJS / r.js

This isn't about CSS, folks. Asset loading is a side-effect of dynamic module loading. Fun to work with, but a complete distraction when talking about the merits of AMD.

@thanpolas I was glad to read a fresh perspective on Require.js in this thread, but I'm wondering if you can elaborate on a few things.

We all are knowledgeable folks here and know what this means or doesn't mean.

I know what this means generally, but I'd like to hear how closure encapsulation is relevant in this context.

You are sending over the wire ~1-3% more bytes, from what your JS app size is. That is too much.

Putting aside the issue of whether 1-3% is "too much", can you qualify these statistics?

@rpflorence I know this isn't about CSS, but I think its relevant in the end for the build process of AMD. I thought it'd be a good idea to bring it to the table even tho it's not the main subject ;)


I know what this means generally, but I'd like to hear how closure encapsulation is relevant in this context.

Well i'll try to reiterate the literature over closures in a very short spin... Creating closures requires responsibility on the developer's part in order to properly handle all the variable declarations that happen inside and outside of a closure. The main reason being giving the Javascript engine a chance to Garbage Collect.

Massive usage of closures will consequently result in memory leaks, if not from you, statistically it's a certainty it'll happen in projects out there, even in ones you might use as third-party dependencies.

Putting aside the issue of whether 1-3% is "too much", can you qualify these statistics?

I can't be exact here, I am mostly guessing but here's a rough estimation...

The requirejs runtime that's included in the production shipped js asset should be around ~5kb of minified code that gzipped can get compressed down to ~2kb.

But the real burden comes from the requirement declarations overhead on each and every file. The smallest declaration one can have is ~57bytes:

define('module', function(require, exports, module) { });

And looking from real life projects, the definitions can get to really crazy sizes that are north of 1kb. An average file with 5 requirements would equal ~130bytes:

define('module', ['moduleA', 'moduleB', 'moduleC', 'moduleD', 'moduleE'],
  function(moduleA, moduleB, moduleC, moduleD, moduleE)) {});

We are talking large-scale projects here so a modest estimation of files included is 1,000. That results in 130kb of declaration overhead on your codebase. Minify and gzip that and you can't get lower than 20kb.

Would you agree with my assumptions?


I don't think 1-3% overhead is a worthy argument against Require.js or any module system. If it increases developer productivity by more than 5% then I would argue it's worth it, though the real question is 5% more efficient than what. 99% of the time, there are better things to optimize.

I don't think the closure argument is valid either since modules that work globally (like jQuery) use closures anyways.
And memory leaks due to closures shouldn't be the responsibility of the module system, it should be the responsibility of the developers.

That being said, I think dependencies should be handled by the build process, not during run time, which would reduce this overhead.

@jonathanong my post right before yours gives a more in depth answer to the exact issues you raise.

@thanpolas i'm not saying you're wrong. In fact, I agree. i just don't think these are a significant arguments against using Require.js.

AMD pattern is what Promise system should have been if it was designed for greater purpose:

Synchronizing arrival of multiple asynchronous resources

As a module loader it's just another module loader. Does not stand out there except for extra elegant dependency declaration mechanism.

What AMD loaders are good at is waking you up when all of the pieces - text templates, business logic, data - arrive into browser and are ready for use.

AMD's genius is in making the timing of things easy. Without that, you would be still slapping together "thread ready" semaphores and adding Promises by hand.

Managing your own dependencies is not bad (as the original author pointed out) as long as

  • you know what you're doing
  • aren't afraid to use tools like YSlow
  • the project is small / less complex

When you have a large project with multiple javascript includes and the page itself is composed in several 'layers' (with a master page and content pages), a more robust solution is called for.

AMD is just such a solution.

I provides for:

  • a flexible script inclusion pattern
  • lazy loading (or 'just in time' loading depending on how you're thinking about it)
  • dependency checking that is much less brittle than 'manual ordering'
  • 'component' or 'modular' centric design.

When working with a medium to large team of developers, this list of benefits will give you pause.

rwaldron commented Feb 8, 2013

cc myself

alex-pi commented Feb 8, 2013

For the record, you CAN debug RequireJS modules (also Dojo's). I did that in my last project.

When the project was finished, I realized that even n00b programmers created well grained modules and reused them when possible.

Some have implied in this thread that require.js can also load CSS files, yet the require.js FAQ says no. If it cannot, this is a pretty huge elephant in the room. I want something that will include ALL my dependencies, not just JS files.

@salient1 for CSS includes you can use a plugin like Require-CSS. There is also LESS support and it works with builds.

Hi all, I finished a proof of concept for a new way of dependency management. It's a working solution that allows discreet, unobtrusive and powerful dependency management. I'll quote from the README of Mantri.

Mantri is...

  • A Robust and discreet Dependency Management System.
  • Synchronous. Everything is done before DOMContentLoaded event triggers.
  • A Grunt plugin.
  • A command line tool (soon).
  • Cross-browser.

Mantri does not...

  • Dictate how you write your code. Vanilla JS, AMD, commonJS, knock yourselves out.
  • Need any runtime on your production file.
  • Need any dependency declarations in your production file.
  • Have any problem moving around JS files or folders.
  • Have any problem working with other dependency systems.
  • Polute your namespace. But you are free to if you want.

... I'll be going through documentation and a blog post announcing the tool in the near future and would welcome any early feedback you may have.

kamov commented Jun 15, 2013

Good reading... thanks to all.

In conclusion?

crdesai commented Jun 26, 2013

Great reading!
I am interested in "changing modes" section in the original post. If we want to live debug in production environment if somethingis not working right, how can this be achieved using requireJS? Any ideas will be great.

Is this not a good candidate for a Stackoverflow question?

Yet another issue with require.js and the like: The DOMContentLoaded-event fires as soon as all the originally embedded scripts have loaded and the original DOM of the document is present. Since injected files or scripts loaded via XHR are not included in this, they will not be present at this time. This results in breaking the load process and deferring the time the page is ready, effectively leading to longer page load and parsing times. Even more, the scripts will compete for network-slots with other embedded assets (images, fonts, etc), slowing them down yet again.

So JS is NOT where you should manage your dependencies, use build-tools or server-side includes to manage them.
Regarding page load times, attempting to handle dependancies via JS always comes at the expense of your users and the general performance of your site. Especially, if the actual page is to be rendered via JS, resulting effectively in a second load cycle for the assets inlined by the generated code. Using some partials loaded via XHR would result in a third cycle and so on.

(Remember late 1990s, how fast the web used to be then?)

@masswerk - I think you should try to work more with require.js. Your statements are a bit flawed.
With require, your main/app module waits for dependencies to load before doing any work, so the Dom content loaded event isn't an issue (usually I put my dom ready event registry inside my main module)

Usually this isn't an issue when in production, as require.js is a build tool, and will concat your js into 1 file. (usually you'll end up with multiple xhr script loading only when in development).

The asynchronous loading aspect is beneficial when you have a really large site, and want to load features/packages only when needed. E.g. I have a labs site, which is a SPA and has a webgl demo link. I don't include Threejs in my minified js, as I only want to load it when someone clicks on my webgl demo link.

Take a look at the optimization documentation:

@jasonmcaffee - Hm, I've seen some require.js in production using still JS-managed dependencies. On the other hand, using a completely other load-cycle-model in testing than in production isn't the best either. (Who is bothering about this anymore at all? E.g.: Developers have eventually become used to jQuery and $(document).ready in such extent that they even don't know that they could have loaded their data already in the head section and could have all things calculated and ready for some time even before the event fires. And using tools like require.js keeps you even more from considering these things, as they're triggering late while in the development phase.)
Fact is, we're all using computers that are beasts, only the NSA would have access to just some 20 years ago, and are using the best optimized software that ever existed (JS engines). And using this, there are just too many projects, where you can watch pages rendering like it were the mid 1990s.

Here's a model to speed up web apps:

  1. Include a source-tag to the data-set server-side
  2. Use something like Plexer.js ( ) to spawn your model-logic to a concurrent process
  3. Have your model ready at DOMContentLoaded
  4. Have more tiny scripts (they will be cached and will be available instantaneously) and not a single, compiled one for every page-type (so the browser will have to load any additional bits only)

A little off topic, but an interesting project I've been using, is jam.js. Been loving it for managing JS libraries and packaging them to be used. Uses the Require framework, but adds some functionality to it. Figured some people on here may find it interesting.

bradw2k commented Aug 22, 2013

Most medium-sized web apps (which is what most of us are building, right?!) do not need the syntactic vomit that RequireJS sprays all over the project. It is not hard to get even several dozen <script> tags in the right order. It. Is. Not. But it is at least as difficult to maintain RequireJS config files and the require() and define() statements. I have seen devs spending hours pairing to figure out their RequireJS config files.

Furthermore, many web apps will simply not benefit from lazy loading, because they will require loading almost everything at a certain choke point -- such as immediately after user login. If you can determine a clear choke point like this, it should be straight forward to configure the script bundling accordingly. Or maybe all of your custom scripts minify to 50K -- in which case you are insane to try to optimize performance by breaking that apart. Now, of course there will be apps where lazy loading will be beneficial. But I think they will be a small minority, based on the medium-sized business apps I have worked on.

Bottom line is that good devs will actually figure out whether or not it makes sense to use RequireJS on a project -- because it is not free. It complicates your ECMAScript5 code, despite what the sycophants say. People who spout that RequireJS should be used as a rule are either sheeple or self-congratulating hipster "experts."

There is no benefit:

...only liabilities (and lies). ;)

albanx commented Sep 30, 2014

Great discussion. I am very interested in understanding the benefits of requirejs. Still after reading this discussion I cannot understand the benefits (for serious web developers).

For example someone says "With RequireJS ... don't have to worry about keeping track of dependencies or load order". Normally I use to develop with objects in JS so in my JS I can load:

<script src="object1.js"></script>
<script src="object3.js"></script>
<script src="someframework(jquery).js"></script>
<script src="object2.js"></script>

The order does not matter, if objects does not depend from each other.
But if objects will depend (object3 extends object2 that extends object1) from each other then I should load them in order, so I can just do:

<script src="object1.js"></script>
<script src="object2.js"></script>
<script src="object3.js"></script>
<script src="someframework(jquery).js"></script>

And that is fine, because so I know the entire flow of my app ( If I am the developer I should know how my app works, right?!!, because I am not the developer then I would not know the dependecies)

@albanx in requirejs you have the control too. Exists the dependiencies of the libraries. Look at this: shim and deps block.

RequireJS is a way to have the code organized and it avoids to use libraries without previous dependency.

samvdb commented Jun 5, 2015

After using RequireJS in several larger projects i would advise AGAINST the use of requirejs. The promise of automatic dependency handling sounds like music in your ears but you will encounter so many problems when actually using it. From async loading problems, shim configurations / config hacks to make a library work to just plain frustration. It simply is the worst opensource project in existence and i would rather use internet explorer 5 for the rest of my life than requirejs.
Do yourself a favor and don't touch it. It adds so many levels of complexity to your project that might already be complex enough. Write plain javascript files and use a taskrunner like grunt to concat / minify the libraries. /endrant

@samvdb Similar experience here.

RequireJS is a prime example of the hell that front-end development has become, it is now harder to develop a JavaScript application than building a thousand file C project. I've worked with many languages and for the past few years JavaScript and its environment have been the constant outlier when it comes to nonsensical libraries and tools that get in the way and make simple work a nightmare for the developer who does not know all the ins and out of the library currently in favour, a library that is often poorly documented and not developer friendly.

We should heavily encourage developers to stay away from such libraries as they promote coding practice that are nefarious, not adapted to JavaScript, encourages laziness and poor knowledge of your own code base.

gnulnx commented Jan 6, 2016

Joined a medium size team on a medium sized project using requirejs. Total nightmare. It's like all my prior experience of web programming (15 years) on the front end has been tossed aside. I have require modules not properly loading similar to the second comments in this thread: Oh sure when it works it just works...but then so do script tags...and once they are ordered they stay ordered. Plus modern web frameworks like django let you compartmentalize functionality into templates.

And while I'll admit that this project likely isn't using requirejs properly I also haven't seen anything that makes me even remotely believe it's worth using. Personally I would never in a million years introduce requirejs into a project. There are so many other traditional way's to handle these issues that it just doesn't make sense.

Software engineers really should know how code works and not try to abstract away every last detail.

philer commented Jan 14, 2016

This thread made me think about my current practice and helped me decide to continue not using require.js.

I don't think require.js is bad. To me it seems more like a habit. You can use require.js to manage your dependencies or you can use something else or you can do it yourself. Doesn't seem to cause much issues either way.

If, and only if, your client side application actually consists of more than several dozen individual js files then automated dependency management will certainly have its benefits. On one project we currently have around 40 modules (some of them global, some only used on specific pages) and some of the meta modules are only supposed to be loaded (i.e. their factory function executed) when they are actually needed. For that purpose I wrote our own dependency manager which has less than 50 lines of js.

In order to get all the dependencies to the client we use grunt to uglify all the modules into one monolithic file. For development the files are simply concatenated without uglifying. The Gruntfile is the only place where all the modules are listed and it is pretty easy to figure out in which order they belong (core / meta modules first, page-specific modules at the end). Even if we wanted to include the source files individually we could do that relatively simply by extracting the module list into a json file and including them via our server side template engine.

Now this is only my setup for a bigger project. Obviously there are even larger projects out there, but considering how easy it was to set up our own dependency management, which is tailored to our particular needs, I doubt that an even larger project couldn't handle that.

However I have a lot of much smaller (client side) projects where I only need anywhere between 3 and maybe 20 js files. I assume that most projects on the web are of this scale, even though they don't like to admit this. After all, if your client side actually uses hundreds of non-trivial modules chances are your site will become slow as f*ck. For projects like that I hardly think your dependency graph will reach any significant complexity. Just create a single global object as your application's namespace (I usually give it a project-specific name to avoid collision with generic names like app). Wrap all your modules into an IIFE and inject the modules into your namespace. Looks like this:


(function(global) {
  "use strict";
  global.my_app = {
    // some useful shortcuts
    $win: jQuery(global),
})(window || this);


(function(app) {
  "use strict";
  var mod = app.SomeModule = {
    // ...

You can also do the class-like approach like this:


(function(app) {
  "use strict";
  var SomeClass = app.SomeClass = {
    // ...
  SomeClass.prototype = Object.create(app.ParentClass, {
    // properties

I've done a fair few projects like this and I've never run into any issues.

A few years have passed since this was started, so there are far more options now. However I wanted to share how RequireJS is still an excellent tool for medium'ish (maybe large to some people) sized projects.

We use RequireJS on a daily basis in an e-comm project (similar to ATG) and I love it. Was the initial config a little tricky? Yes but after that it's been excellent. That tricky part is in reference to our need to create a modified version of the multi-page with shim support pattern to make it fit our needs - the original pattern didn't work due to our existing codebase/file structure. For an idea of the project complexity front and back: the project has north of 600 JS files (there are the usual inline scripts as well that aren't counted) and north of 3,000 class files (src).

Using RequireJS in the way we've implemented it has allowed us to progressively modularize the application, which was only realistic option. To say it's flexible would be an accurate statement as it's currently supporting 2 modes of operation in production - dist and src (which we can toggle for debugging). We run r.js using clojure compiler and node uglify2, since both were necessary to allow teams without node to compile the project (they just need to wait a minute or 2 😄 ). We're also in the early stages of a service based approach for our client-side assets that are tied to RequireJS i.e. imagine a 3rd party that specializes in writing the front-end code for your site and delivering it via Akamai/Verizon/some CDN. Network throttling tests over the HTTP and HTTPS schemes has gone smoothly (scaled back to dial up speeds) and we've added transpiling into the mix so we can stay current (maps provided via Babel 6).

In summary, RequireJS is the exit point for most of the platform JS in a hybrid (2-3 mincat'd scripts in dist) and modular (src) distribution, and it's doing what it was designed to do flawlessly.

I totally agree with what @philer stated, I've use the same setup in several projects with 20 to 30 js dependencies and haven't run into any issues. Not to say that RequireJs isn't a great library.

require.js is a dependency to manage your dependencies.

danielsokolowski commented Oct 10, 2016

require.js is layer that is not needed, it tries to solve a problem than one can explicitly resolve using <script> tags. It stems from the lack of recognition that on the frontend JS is a second class citizen - the author of the question is right to ask the 'why' and should stick to what is simpler to them.

There is of course positive though, it and other projects like it have served their purpose. They highlight that there is a desire (not to be confused with need) for JS module loading on the front end and browser community has listened; I invite the reader to google 'ECMAScript 6 modules'

nezarat commented Dec 9, 2016

Yes I'm agree with questioner. Why we should learn a lot of framework's syntax and usages while we can do it easily and purely with java-script itself and JQuery! require, backbone, underscore, ..... and thousands of frameworks!!! if you are a developer you can think and use your mind :) such a problems are really easy to solve, browsers guaranteed loading scripts and css files in order of their declaration! load libraries like jquery at first and then your custom scripts ... even you can load see below:

$.getScript( "ajax/test.js" )
.done(function( script, textStatus ) {
console.log( textStatus );
.fail(function( jqxhr, settings, exception ) {
$( "div.log" ).text( "Triggered ajaxError handler." );

don't fill your mind with unnecessary things!!!

I read each and every post above from 2013 to now. The general trend I'm noting as time progresses is to avoid using requirejs. Is that fair approximation?

A fair approximation would be that modules have been a problem for a decade and we only have es6 module solution on paper... If you are loading modules asynchronously you still going to need requirejs. But with a typescript compiler its pretty transparent and the compiler takes care of the boilerplate code and sourcemaps. My only complaint about requirejs is that its 26KB minified.

To browser vendors not implementing es6 modules, **** ***

onigetoc commented Dec 4, 2017

What i see it sometime and often, one person develope a small app and use require without knowing why is using it. Just because everyone do it.

My biggest concern about requirejs/AMD and the well-known (distributed) libraries is that they assume the AMD implementation knows how to identify the module. This by using a path/url as identifier.

For example:


define([], function(){  
   return {
      use: function() {}


define(['/vendor/DistributedLib'], function(lib){  

That's all fine and dandy, but what happens if we concatenate the files:

define([], function(){  
   return {
      use: function() {}
define(['/vendor/DistributedLib'], function(lib){  

Our dependency suddenly lost it's id! This piece of code no longer works.

The AMD specification allows setting an id as first parameter but I notice no one sets it in their distributed libraries. Most developers assume that if define && define.amd exists we are using requirejs / path-identifiers. IMHO the id should always be set unless it is an anonymous module:

define('DistributedLib', [], function(){  
   return {
      use: function() {}
define(['DistributedLib'], function(lib){  

Some time ago I created an AMD implementation (smd.js) since I didn't want file-loading. I was pretty baffled to find out that however I followed the spec, 99% of the libraries out there are not compatible. This because of the anonymous declarations.

Maybe I'm totally wrong here (and not seeing the bigger picture), but it seems to me the AMD pattern is not being used properly (unless you are using requirejs-like systems)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment