Confusing?  CommonJS, AMD, RequireJS, UMD, Modules in ES Harmony

A much more comprehensive documentation is written by Addy Osmani:

Writing Modular JavaScript With AMD, CommonJS & ES Harmony

All of these terms are related to manage growing complicated JavaScript source code in both client and server sides for better software engineering purpose.

A. How JavaScript is loaded into browser?


  1. By the <script src="”source.js”" async="" defer="defer">
    tag. You can defer it, or make it asynchronous use the new attributes, although buggy.
  2. Use document.write:
    var tag = <script src="”source.js”" ></script >
  3. Use appendchild(script):
    var script = document.createElement(‘script’);
    script.src = “”;
  4. XHR and eval:
    $.ajax({url: url}).done(function(js){eval(js);})
  5. In Chrome, use <link rel=”prefetch” > to load resources
  6. Script loaders: CommonJS, AMD, RequireJS, curl.js, UMD, etc..

B. So, How, When And Why Script Loaders Are Appropriate

Most times, just use the traditional method to minify and concatenate all JavaScript files into a single file and put it just before </body> tag.

C. CommonJS

CommonJS defines a module format such that it works on both client and server side. The drawback for CommonJS in web browser is that it won’t simply allow asynchronous loading.


The Asynchronous Module Definition (AMD) API specifies a mechanism for defining modules such that the module and its dependencies can be asynchronously loaded.

The signature function: define(id?, [dependencies?], factory)

E. RequireJS

It implements AMD and caters for simplified CommonJS wrapping.


The UMD pattern typically attempts to offer compatibility with the most popular script loaders of the day (e.g RequireJS amongst others). In many cases it uses AMD as a base, with special-casing added to handle CommonJS compatibility.

G. Modules in ES5

An ES6 module is a file containing JS code. There’s no special module keyword; a module mostly reads just like a script. There are two differences.

  • ES6 modules are automatically strict-mode code, even if you don’t write “use strict”;in them.
  • You can useimport and export in modules.

You differentiate the normal script and module by giving type attribute in the script tag,

i.e.: set script tag with type=”module”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s