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)

Discover More
CommonJs (Node.js) - Module.exports

If you want the commonJs/Node module to be an instance of some class, assign the desired export object to the module.exports property. Note that assigning the desired object to exports will simply rebind...
CommonJs (NodeJs) - Require

The import functionality implementation of commonjs (and then node) is done via the require function require basically: reads a Javascript file (generally a CommonJs module but it may be any javascript...
Welcome From Browser
How to develop, publish and use a javascript library ?

A step by step tutorial on how to create and publish a javascript library
How to resolve the SyntaxError: Cannot use import statement outside a module?

This article shows you how to resolve the syntax Error: Cannot use import statement outside a module.
Javascript - Module

functionality in Javascript. A module is different from a script file because: it defines a well-scoped object that does not pollute the global namespace. It list explicitly its dependencies Javascript...
Javascript - Module Loader (Script Loader)

A loader searches and loads module and script into the javascript engine (environment). From the main script, they will: create a dependency graph from the require and import statement find them...
Javascript - Universal Module Definition (UMD)

The UMD module format try to be the glue between differents modules definition by offering template A UMD module wraps the following module format: a Global Variable module the CommonJS format...
Javascript ES - Module Script (esm / .mjs)

The module implementation in Ecma Script (ES2015). An ES6 module is a Javascript file: automatically set in strict-mode with export statement and / or statement Everything inside a module is...
Javascript Module - Dependency Graph

This page is the dependency graph between module. The dependency graph is build from a single module (called the entry point) for a ESM module with the import statement for a commonJs module (node...
Javascript Module - Import (Es Module)

This page is the import statement defined in the es module specification The ES6 import is: a function for dynamic import (performed at runtime time) or a statement for static import (performed...

Share this page:
Follow us:
Task Runner