Javascript - CommonJs


CommonJs 1) 2) is a Module format definition

They have defined:

The CommonJS module proposal specifies a simple API for declaring modules server-side (and unlike AMD) attempts to cover the server side with io, filesystem, promises and more. Due to its history and implementation 3) , using CommonJS modules in the most common of JS environments, the browser, is non-optimal today.

The commonJS format is the default format used by Node.js. Therefore a CommonJS module will run on node.


The CommonJS (CJS) list participants 4) have worked out the module format.

The CJS mechanisms contain two primary parts:

  • export: a variable named exports that contains the public API
  • import: a require function that load the module and return the exports variable.

A CJS module does not use any function wrappers such as the AMD define to specify an id or dependencies.

  • The module id is the path or module name via a search path
  • The dependencies are declared via the require function.


Import is done via the require function

// require will import code from another module
var MyDependency = require('my-dependency');


The module exposes its public api via the exports property

// module.exports will make its property public 
exports = ...;




There is not so much adoption of CJS in the browser because:

  • many CommonJS APIs address server-oriented features that could not be implement in a browser (for example, io, system and js)
  • amd defines already a require function (bringing conflict and confusion)

There was loader but most of them are now no more supported

  • curl.js (discontinued 8 years ago - 2014)

Powered by ComboStrap