The `module` Object

Added in: v0.1.16. It is <Object>. In each module, the module free variable is a reference to the object representing the current module. For convenience, module.exports is also accessible via the exports module-global. module is not actually a global but rather local to each module.

module.children

Added in: v0.1.16 It is <module[]>. The module objects required for the first time by this one.

module.exports

Added in: v0.1.16 It is <Object>. The module.exports object is created by the Module system. Sometimes this is not acceptable; many want their module to be an instance of some class. To do this, assign the desired export object to module.exports. Note that assigning the desired object to exports will simply rebind the local exports variable, which is probably not what is desired.

For example, suppose we were making a module called a.js:

const EventEmitter = require(‘events’);

module.exports = new EventEmitter();

// Do some work, and after some time emit  the ‘ready’ event from the module itself.

setTimeout(() => {

module.exports.emit(‘ready’);

}, 1000);

Then in another file we could do:

const a = require(‘./a’);

a.on(‘ready’, () => {

console.log(‘module “a” is ready’);

});

Note that assignment to module.exports must be done immediately. It cannot be done in any callbacks. This does not work:

x.js:

setTimeout(() => {

module.exports = { a: ‘hello’ };

}, 0);

y.js:

const x = require(‘./x’);

console.log(x.a);

exports shortcut – Added in: v0.1.16. The exports variable is available within a module’s file-level scope, and is assigned the value of module.exports before the module is evaluated.

It allows a shortcut, so that module.exports.f = … can be written more succinctly as exports.f = …. However, be aware that like any variable, if a new value is assigned to exports, it is no longer bound to module.exports:

module.exports.hello = true; // Exported from require of module

exports = { hello: false };  // Not exported, only available in the module

When the module.exports property is being completely replaced by a new object, it is common to also reassign exports:

module.exports = exports = function Constructor() {

// … etc.

};

To illustrate the behavior, imagine this hypothetical implementation of require(), which is quite similar to what is actually done by require():

function require(/* … */) {

const module = { exports: {} };

((module, exports) => {

// Module code here. In this example, define a function.

function someFunc() {}

exports = someFunc;

// At this point, exports is no longer a shortcut to module.exports, and

// this module will still export an empty default object.

module.exports = someFunc;

// At this point, the module will now export someFunc, instead of the

// default object.

})(module, module.exports);

return module.exports;

}

The module.exports or exports is a special object which is included in every JS file in the Node.js application by default. module is a variable that represents current module and exports is an object that will be exposed as a module. So, whatever you assign to module.exports or exports, will be exposed as a module.

Let’s see how to expose different types as a module using module.exports.

Export Literals – As mentioned above, exports is an object. So it exposes whatever you assigned to it as a module. For example, if you assign a string literal then it will expose that string literal as a module. The following example exposes simple string message as a module in Message.js.

Message.js

module.exports = ‘Hello world’;

//or

exports = ‘Hello world’;

Now, import this message module and use it as shown below.

app.js

var msg = require(‘./Messages.js’);

console.log(msg);

Run the above example and see the result as shown below.

C:\> node app.js

Hello World

You must specify ‘./’ as a path of root folder to import a local module. However, you do not need to specify path to import Node.js core module or NPM module in the require() function.

Export Object – exports is an object. So, you can attach properties or methods to it. The following example exposes an object with a string property in Message.js file.

Message.js

exports.SimpleMessage = ‘Hello world’;

//or

module.exports.SimpleMessage = ‘Hello world’;

In the above example, we have attached a property “SimpleMessage” to the exports object. Now, import and use this module as shown below.

app.js

var msg = require(‘./Messages.js’);

console.log(msg.SimpleMessage);

In the above example, require() function will return an object { SimpleMessage : ‘Hello World’} and assign it to the msg variable. So, now you can use msg.SimpleMessage.

Run the above example by writing node app.js in the command prompt and the output is as

C:\> node app.js

Hello World

The same way as above, you can expose an object with function. The following example exposes an object with log function as a module.

Log.js

module.exports.log = function (msg) {

console.log(msg);

};

The above module will expose an object- { log : function(msg){ console.log(msg); } } . Use the above module as shown below.

app.js

var msg = require(‘./Log.js’);

msg.log(‘Hello World’);

Run and see the output in command prompt as shown below.

C:\> node app.js

Hello World

You can also attach an object to module.exports as shown below.

data.js

module.exports = {

firstName: ‘James’,

lastName: ‘Bond’

}

app.js

var person = require(‘./data.js’);

console.log(person.firstName + ‘ ‘ + person.lastName);

Run the above example and see the result as shown below.

C:\> node app.js

James Bond

Export Function – You can attach an anonymous function to exports object as shown below.

Log.js

module.exports = function (msg) {

console.log(msg);

};

Now, you can use the above module as below.

app.js

var msg = require(‘./Log.js’);

msg(‘Hello World’);

The msg variable becomes function expression in the above example. So, you can invoke the function using parenthesis (). Run the above example and see the output as shown below.

C:\> node app.js

Hello World

Export function as a class – In the JavaScript, a function can be treated like a class. The following example exposes a function which can be used like a class.

Person.js

module.exports = function (firstName, lastName) {

this.firstName = firstName;

this.lastName = lastName;

this.fullName = function () {

return this.firstName + ‘ ‘ + this.lastName;

}

}

The above module can be used as shown below.

app.js

var person = require(‘./Person.js’);

var person1 = new person(‘James’, ‘Bond’);

console.log(person1.fullName());

As you can see, we have created a person object using new keyword. Run the above example as below.

C:\> node app.js

James Bond

In this way, you can export and import a local module created in a separate file under root folder.

Node.js also allows you to create modules in sub folders. Let’s see how to load module from sub folders.

Load Module from Separate Folder – Use the full path of a module file where you have exported it using module.exports. For example, if log module in the log.js is stored under “utility” folder under the root folder of your application then import it as shown below.

app.js

var log = require(‘./utility/log.js’);

In the above example, . is for root folder and then specify exact path of your module file. Node.js also allows us to specify the path to the folder without specifying file name. For example, you can specify only utility folder without specifing log.js as shown below.

app.js

var log = require(‘./utility’);

In the above example, Node will search for a package definition file called package.json inside utility folder. This is because Node assumes that this folder is a package and will try to look for a package definition. The package.json file should be in a module directory. The package.json under utility folder specifies the file name using “main” key as below.

./utility/package.json

{

“name” : “log”,

“main” : “./log.js”

}

Now, Node.js will find log.js file using main entry in package.json and import it. If package.json file does not exist then it will look for index.js file as a module file by default.

module.filename

Added in: v0.1.16 It is <string>. The fully resolved filename of the module.

module.id

Added in: v0.1.16 It is <string>. The identifier for the module. Typically this is the fully resolved filename.

module.loaded

Added in: v0.1.16 It is <boolean>. Whether or not the module is done loading, or is in the process of loading.

module.parent

Added in: v0.1.16 It is <module>. The module that first required this one.

module.paths

Added in: v0.4.0 It is <string[]>. The search paths for the module.

module.require(id)

Added in: v0.5.1

  • id <string>
  • Returns: <any> exported module content

The module.require() method provides a way to load a module as if require() was called from the original module. In order to do this, it is necessary to get a reference to the module object. Since require() returns the module.exports, and the module is typically only available within a specific module’s code, it must be explicitly exported in order to be used.

Share this post
[social_warfare]
The module wrapper
The `Module` Object

Get industry recognized certification – Contact us

keyboard_arrow_up