A module has a unique absolute id. The same module may be resolved by require(String) for different path arguments, like relative paths (starting with "./" or "../"), or absolute paths. See the documentation of require for more details about the lookup procedure.
Every module object has an exports property which can be used by the module implementation to expose its public functions or properties. Only functions and properties that are explicitly exported are accessible from other modules, all others are private and not visible. For convenience, the global exports variable is by default also initialized with the module.exports property of the current module. In the most simple case, module elements can be exposed by adding them to the exports object, like:
// Greeting.js exports.sayHello = function() { return 'Hello World!'; };This is equivalent to:
// Greeting.js module.exports.sayHello = function() { return 'Hello World!'; };With the above implementation, a caller (for example another module in the same directory) could call the module function like this:
var message = require('./Greeting').sayHello();It is also possible to replace the whole module exports object with a completely different value, for example with a function:
// Greeting.js module.exports = function sayHello() { return 'Hi!'; }Now the result of require would be a function, which can be invoked directly like:
var message = require('./Greeting')();This construction can be used for exporting constructor functions, so that a module becomes something like a class:
// Greeting.js function Greeting() { this.message = 'Hi!'; } Greeting.prototype.getMessage = function() { return this.message; } module.exports = Greeting;which would be used like:
var Greeting = require('./Greeting'); var m = new Greeting().getMessage();