CommonJS - Node.js

suggest change

CommonJS is the module system Node.js used by default for years. Current Node.js also supports ECMAScript modules.

The CommonJS system is centered around a require() function that loads other modules and an exports property that lets modules export publicly accessible methods.

This example loads Lodash and Node’s fs module. require is synchronous and resolves from the file that calls it:

// Load fs and lodash, we can use them anywhere inside the module
var fs = require("fs"),
    _ = require("lodash");

var myPrivateFn = function(param) {
    return "Here's what you said: " + param;
};

// Here we export a public `myMethod` that other modules can use
exports.myMethod = function(param) {
    return myPrivateFn(param);
};

You can also export a function as the entire module using module.exports:

module.exports = function() {
    return "Hello!";
};

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents