CommonJS

In notebook:
FrontEndMasters API design
Created at:
2016-03-13
Updated:
2016-03-13
Tags:
backend JavaScript
RequireJS actually uses AMD modules. Even though CommonJS uses the ​require()​ syntax.

Node uses CommonJS. We get access to third-party modules and their apis.
  var path = require('path');
Path module (above) is built into node. In this case you don't need to add a path to your module. Same if the module you require is in the node_modules/ directory (no need to add path).

The path is relative to the file you are requiring from. No extension is necessary.
To expose your api use the ​exports​ object.
  exports.setup = function(){};
exports.enable = function(){};

// or

module.exports = {
    setup: function(){},
    enable: function(){}
}
​exports​ is a global variable in NodeJS. 
when you add a file, NodeJS automatically wraps it in an IFEE, and passes it some global variables: ​module, exports, __dirname
Inside your IFEE, you have access to all these variable (there are a few more, he doesn't specify)
- will nodeJS support ES6 kind of modules?
- it surely will since it's using the V8 engine and it will support it

- pros and cons of ​module.exports​ or ​exports
- you can do ​module.exports​ once in your file. ​exports​ is always an Object, whereas you can ​module.exports = "foo"​ (so it's your module is a String)