Showing posts with label module.exports. Show all posts
Showing posts with label module.exports. Show all posts

Saturday, 22 December 2018

node.js: module.exports vs exports

In this post, I am going to explain the difference between module.exports and exports.

Whenever you load a module using require() function, it returns module.exports object of that module.

For example,
util.js
module.exports = {
	productName : "chat server",
	version : "1.2.3"	
}

index.js
util = require("./util.js")

console.log(`name : ${util.productName}`)
console.log(`version : ${util.version}`)

Execute the command ‘node index.js’, you can see below messages in console.

name : chat server
version : 1.2.3

What is exports?
exports is a reference to module.exports. If you attach any property to exports those are attached to module.exports also.


util.js
module.exports.productName = "chat server"

// Adding properties using exports
exports.version = "1.2.3"
exports.vendor = "ABC Corporation"

index.js
util = require("./util.js")

console.log(`name : ${util.productName}`)
console.log(`version : ${util.version}`)
console.log(`vendor : ${util.vendor}`)

Run index.js, you can see below messages in console.

name : chat server
version : 1.2.3
vendor : ABC Corporation

if module.exports pointing to some other object directly, then the properties exposed by export are ignored.


util.js
module.exports = {
	productName : "chat server"
}

// Adding properties using exports, these will be ignored
exports.version = "1.2.3"
exports.vendor = "ABC Corporation"

index.js
util = require("./util.js")

console.log(`name : ${util.productName}`)
console.log(`version : ${util.version}`)
console.log(`vendor : ${util.vendor}`)

Output
name : chat server
version : undefined
vendor : undefined


As you see the output, vendor and version details are undefined. It is because, module.exports is assigned to another object directly. So whatever you exposed via export will be ignored.


Previous                                                 Next                                                 Home

JavaScript: Explain about module.exports

Whenever you load a module using require() function, it returns module.exports object of that module.

Let me explain with an example.

models.js
function Employee(firstName, lastName, addr){
	this.firstName = firstName;
	this.lastName = lastName;
	this.addr = addr;
	
	this.printInfo = function(){
		console.log("firstName : " + this.firstName);
		console.log("lastName : " + this.lastName);
		this.addr.printInfo();
	}
}

function Address(city, country){
	this.city = city;
	this.country = country;
	
	this.printInfo = function(){
		console.log("city : " + this.city);
		console.log("country : " + this.country);
	}
}

module.exports.Employee = Employee;
module.exports.Address = Address;

As you see models.js, I exported Employee and Address functions. So whoever loading the module models.js using require function, those can acceess Employee and Address constructor functions.

module.exports.Employee = Employee;
module.exports.Address = Address;

app.js
var models = require("./models.js");

var Employee = models.Employee;
var Address = models.Address;


var addr = new Address("Bangalore", "India");
var emp = new Employee("Krishna", "Gurram", addr);

emp.printInfo();

$node app.js
firstName : Krishna
lastName : Gurram
city : Bangalore
country : India

You can even export the functions, types or anything in the module using object literal notation like below.

module.exports = {
         Employee : Employee,
         Address : Address
}

Example 2

Emp.js

function Employee(firstName, lastName){
	this.firstName = firstName;
	this.lastName = lastName;
	
	this.printInfo = function(){
		console.log("firstName : " + this.firstName);
		console.log("lastName : " + this.lastName);
	}
}

module.exports = Employee;

App.js

const Employee = require("./Emp.js");

var emp = new Employee('Krishna', 'Gurram');

emp.printInfo();

Output
firstName : Krishna
lastName : Gurram

While loading the module using require function, you no need to specify .js file extension.

const Employee = require("./Emp");




Previous                                                 Next                                                 Home