CommonJS is a module format that is more optimized for server side programming. CommonJS is used as the standard module definition mechanism in Node.js.
It provides a very simple API for defining and importing a module.
There are just two objects that are needed:
exports: A global variable that is used to define a modulerequire: A global variable that used to import an external module for use
Creating a module is as simple as creating an object/function and then exposing
it via the exports variable.
Note that for constructors it is possible to export a module both before and after actually defining it. This works because of hoisting.
For objects we must define the object before attaching to exports otherwise
undefined will be assigned.
Path used to require is relative to the current file.
For example, we create a module file foo.js as below:
//create and export the module
exports.foo = function {
console.log('foo');
};Module is imported using the require global method.
Assuming bar.js in the same folder as foo.js above.
//importing module
var lib = require('./foo');
//calling
lib.foo();Node.js module export works differently for objects and Constructors.
To export objects augment the global exports object directly.
exports.foo = function () {}
var bar = {
some:'112',
other: function() {}
};
exports.bar = bar;However, constructors cannot be exported using exports directly.
We need to use the module.exports to be able to export constructors.
function bar() {}
//exports = bar; //fails
//this works
module.exports = exports = bar;