Understanding Require and Exports in Nodejs

Posted By : Ravi Verma | 29-Jun-2015
Understanding Require and Exports in Node.js

When we start playing with node.js the thing that makes all node dev's uncomfortable that is module.exports, well it's such a fundamental part of node.js and it's quite simple. In Node, variables, functions, classes and class members are only visible to other variable and functions etc in the same file.

So, here is a file called file.js with the following contents:

var num = 5;
var addNum = function(value) {
  return value + num;
};
        

Here Another file cannot access the num variable or addNum function. It is not as used as of the var keyword. It's Rather, a Node building block called a module which maps directly to a file. So we can say that the given file belongs to a module named file1 and everything within that module (or any module) is private.

Now, here a situatuation raise where we need to expose things out of a module, let's look at loading a module. This is where require play its role as require is used to load a module, which is why its return value is assigned to a variable:

var file = require('./file');
        

Ofcourse, the above isn't very useful as long as our module doesn't expose anything . To expose things we use module.exports and export everything we want:

var num = 5;
var addNum = function(value) {
  return value + num;
};
module.exports.num = num;
module.exports.addNum = addNum;
        

Now our loaded module will go:

var file = require('./file');
console.log("Adding %d to 10 gives us %d", file.num, file.addNum(10));
        

There's another way to expose things in a module:

var Person = function(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
};
module.exports = Person;
        

The difference is subtle but important. Here we are exporting person directly, without any indirection. The difference between:

module.exports.Person = Person;
//vs
module.exports = Person;

it's about how it is used:

var person = require('./person');
var p = new person.Person();
//vs
var p = new person();

But here you need to understand whether your module is a container of exported values or not. You can mix the two within the same module, but that will lead to an ugly API.

Finally, the thing that needs to consider is what happens when you directly export a function:

var evenOrOdd= function(num) {
  return num % 2 ==0 ? "it's Even!!!" : "it's Odd!!!" ;
};
module.exports = evenOrOdd;
        

When you require the above file, the returned value is the actual function. This means that you can do:

require('./evenOrOdd')(9050);
        

Which is really just a condensed version of:

var evenOrOdd = require('./evenOrOdd')
evenOrOdd(100);
        

Hope you find it helpful. Thanks!

About Author

Author Image
Ravi Verma

Ravi is a seasoned technologist with excellent experience in AngularJS , NodeJS and MEAN stack. He has good experience in developing complex UIs for web and mobile applications.

Request for Proposal

Name is required

Comment is required

Sending message..