Sunday 12 November 2017

Node.js | 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 we assign to module.exports or exports, will be exposed as a module.

We can expose different types of modules using module.exports are described below.

Export Literals:
exports is an object so it exposes whatever we assigned to it as a module.
For example, if you assign a string literal then it will expose that string literal as a module.

ExportsString.js

module.exports = 'Hey! We are exposing the String here';
//or
exports = 'Hey! We are exposing the String here';


ImportStringModule.js
var msg = require('./ExportsString.js');

console.log(msg);

Output:
D:\node>node ImportStringModule.js
Hey! We are exposing the String here

Export Object:
We can attach properties or methods to exports object. We are attaching the property to exports.

Export property using exports object
We can expose an object with the property.
Message.js
exports.message = 'Hello world';
//or
module.exports.message = 'Hello world';

MessageTest.js
var msg = require('./Message.js');

console.log(msg.message);

Output:
D:\node>node MessageTest.js
Hello world

Export function using exports object
We can expose an object with function as well.

PrintMessage.js
module.exports.print = function (msg) {
    console.log(msg);
};

TestPrintMessage.js
var msg = require('./PrintMessage.js');

msg.print('Hello World');

Output:
D:\node>node TestPrintMessage.js
Hello World

Export object using exports object
We can also attach an object to module.exports.

EmpObject.js
module.exports = {
    empId: '123',
    empName: 'Algos'
}

ImportObject.js
var employee = require('./EmpObject.js');
console.log(employee.empId + ' ' + employee.empName);

Output:
D:\node>node TestEmpObject.js
123 Algos

Export anonymous Function:
We can attach an anonymous function to exports object as shown below.

AnonymousFunc.js
module.exports = function (msg) {
    console.log(msg);
};

TestAnonymousFunc.js
var msg = require('./AnonymousFunc.js');

msg('Hello World');
The msg variable becomes function expression in the above example. So, we can invoke the function using parenthesis ().

Output:
D:\node>node TestAnonymousFunc.js
Hello World

Export function as a class:
In the JavaScript, a function can be treated like a class.

Employee.js
module.exports = function (empId, empName) {
    this.empId = empId;
    this.empName = empName;
    this.empDetail = function () {
        return this.empId + ' ' + this.empName;
    }
}

EmployeeDetails.js
var employee = require('./Employee.js');

var empIns = new employee('123', 'Debug');

console.log(empIns.empDetail());

Output:
D:\node>node EmployeeDetails.js
123 Debug

Load module from separate folder:
Use the full path of a module file where we have exported it using module.exports.

Exports from separate folder exist in the root directory
If Message.js is stored in "utility" folder under the root folder of our application then import it as shown below.

Full path: D:\node\utility\Message.js
exports.message = 'Hello world';

Full path: D:\node\MessageTest.js
var msg = require('./utility/Message.js');

console.log(msg.message);

Exports from separate folder exist outside the root directory
If Message.js is stored in "utility" folder outside the root folder of our application then import it as shown below.

Full path: D:\utility\Message.js
exports.message = 'Hello world';

Full path: D:\node\MessageTest.js
var msg = require('../utility/Message.js');

console.log(msg.message);



No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...