Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relation between CommonJS, AMD and RequireJS?

I'm still very confused about CommonJS, AMD and RequireJS, even after reading a lot.

I know that CommonJS (formerly ServerJS) is a group for defining some JavaScript specifications (i.e. modules) when the language is used outside the browser. CommonJS modules specification has some implementation like Node.js or RingoJS, right?

What's the relation between CommonJS, Asynchronous Module Definition (AMD) and RequireJS?

Is RequireJS an implementation of the CommonJS module definition? If yes, what's AMD then?

like image 766
gremo Avatar asked May 13 '13 11:05

gremo


People also ask

What do you think of AMD vs CommonJS?

AMD and CommonJS are both Javascript module loader. They accomplish the same task but works different. AMD is better for browser, hence, the name 'Asynchronous', as it loads each distinct module in async manner instead of loading in one large file. No extra steps are required to use AMD, it works out-of-the-box.

What is the difference between CommonJS and ES6?

While CommonJS and ES6 modules share similar syntax, they work in fundamentally different ways: ES6 modules are pre-parsed in order to resolve further imports before code is executed. CommonJS modules load dependencies on demand while executing the code.

What is the difference between ESM and CommonJS?

CommonJS imports are synchronous. ESM imports are asynchronous (which also allows for top-level await ) CommonJS works in Node but does not work in browsers. ESM is supported by all modern browsers and the latest versions of Node, but does not work at all in Node versions below 12.

Is CommonJS a modularity framework?

Although much has changed since 2009, CommonJS paved the way for modern modularity practices that are a requirement of any program written today.


2 Answers

CommonJS is more than that - it's a project to define a common API and ecosystem for JavaScript. One part of CommonJS is the Module specification. Node.js and RingoJS are server-side JavaScript runtimes, and yes, both of them implement modules based on the CommonJS Module spec.

AMD (Asynchronous Module Definition) is another specification for modules. RequireJS is probably the most popular implementation of AMD. One major difference from CommonJS is that AMD specifies that modules are loaded asynchronously - that means modules are loaded in parallel, as opposed to blocking the execution by waiting for a load to finish.

AMD is generally more used in client-side (in-browser) JavaScript development due to this, and CommonJS Modules are generally used server-side. However, you can use either module spec in either environment - for example, RequireJS offers directions for running in Node.js and browserify is a CommonJS Module implementation that can run in the browser.

like image 33
Nate Avatar answered Sep 25 '22 14:09

Nate


RequireJS implements the AMD API (source).

CommonJS is a way of defining modules with the help of an exports object, that defines the module contents. Simply put, a CommonJS implementation might work like this:

// someModule.js exports.doSomething = function() { return "foo"; };  //otherModule.js var someModule = require('someModule'); // in the vein of node     exports.doSomethingElse = function() { return someModule.doSomething() + "bar"; }; 

Basically, CommonJS specifies that you need to have a require() function to fetch dependencies, an exports variable to export module contents and a module identifier (which describes the location of the module in question in relation to this module) that is used to require the dependencies (source). CommonJS has various implementations, including Node.js, which you mentioned.

CommonJS was not particularly designed with browsers in mind, so it doesn't fit in the browser environment very well (*I really have no source for this--it just says so everywhere, including the RequireJS site.*) Apparently, this has something to do with asynchronous loading, etc.

On the other hand, RequireJS implements AMD, which is designed to suit the browser environment (source). Apparently, AMD started as a spinoff of the CommonJS Transport format and evolved into its own module definition API. Hence the similarities between the two. The new feature in AMD is the define() function that allows the module to declare its dependencies before being loaded. For example, the definition could be:

define('module/id/string', ['module', 'dependency', 'array'],  function(module, factory function) {   return ModuleContents;   }); 

So, CommonJS and AMD are JavaScript module definition APIs that have different implementations, but both come from the same origins.

  • AMD is more suited for the browser, because it supports asynchronous loading of module dependencies.
  • RequireJS is an implementation of AMD, while at the same time trying to keep the spirit of CommonJS (mainly in the module identifiers).

To confuse you even more, RequireJS, while being an AMD implementation, offers a CommonJS wrapper so CommonJS modules can almost directly be imported for use with RequireJS.

define(function(require, exports, module) {   var someModule = require('someModule'); // in the vein of node       exports.doSomethingElse = function() { return someModule.doSomething() + "bar"; }; }); 
like image 195
jakee Avatar answered Sep 22 '22 14:09

jakee