Using Modules in Node.js


Node.js has a very simple module usage structure. One can use require to include any module/modules into current JS file. It will load the module mentioned in the quote in require function. Let us see some examples for loading a module in node.js.

Example 1:

Consider the following reusable code maxm.js

// this function finds maximum number out of array of numbers

module.exports = function (numbers) {
    var max = 0 ;
    for (var n in numbers) {
        if (max < numbers[n]) max = numbers[n];
    return max;

In the above code modules.export instructs node.js compiler that the assigned function reference is to be exported to module object. The above code simply accepts array of numbers as argument and returns the maximum number out of it.

Let us use the above reusable code as a module in some js file:

var maxutil = require("./maxm");

var nos = [2,5,6,7,4];

// using single function module
var max2 = maxutil(nos);

console.log ('max:'+max2);

In the above example, require(“./maxm”) loads the module from maxm.js file in the same directory where the above JS file lies. Here when maxutil is called, function body is called which was assigned as reference to modules.export in maxm.js. This function does it’s task to find a max number and returns. The max number is assigned to variable max2 which is printed on the console.

Example 2

Now let us export multiple functions in a given module, assigning a reference name to each of them. Let us consider the code following file numbersmod1.js:

// this module has number utils

// this function find sums of all numbers in an array
var sumOfNumbers = function (numbers) {
    var sum = 0;
    for (var n in numbers) {
        sum += numbers[n];
    return sum;

// this function finds max number out of a given array of numbers
var maxNumber = function (numbers) {
    var max = 0;
    for (var n in numbers) {
        if (max < numbers[n]) max = numbers[n];
    return max;

// exporting the functions to the modules
module.exports.sumOfNumbers = sumOfNumbers;

module.exports.maxNumber = maxNumber;

We have exported two functions maxNumber and sumOfNumbers from this module, respectively, which does the task of finding the max number and sum of numbers. We can export n number of functions from the modules we create. Let us now use this module function in another JS file:

var numberutils = require ("./numbersmod1");
var maxutil = require("./maxm");

var nos = [2,5,6,7,4];

// using multi function module
var sum = numberutils.sumOfNumbers (nos);
console.log ('Sum of numbers:'+sum);

var max = numberutils.maxNumber (nos);

console.log ('Max of numbers:'+max);

As compared to example 4, here we have used numberutils.<function_name> as we have multiple functions exported to this module.

Example 3:

There is one more way to declare multiple functions. Here we shall take arguments in the function reference and we shall return a object which has multiple functions as functionality of the module.

Lets see the following code: mfuncs.js

module.exports = function (personInfo) {
    var data = {
        name : null,
        age : null 
    for (var prop in data) {
        if (data[prop]!= 'undefined') {
            data[prop] = personInfo[prop];
    var functions = {
        getPersonName: function () {
        getPersonAge: function() {
            return data.age;
    return functions;


We can use this module in some other JS file as follows:

var person = {
    name: "parth",
    age: 22

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

// var mf = mfuncs(person);
var mf = require('./mfuncs') (person);

var name = mf.getPersonName();
var age = mf.getPersonAge();


One can observe that line 6-8 is equivalent to line 9. The above is just one more way to have exported multiple functions using facilities given by JavaScript. Here the benefit is that we can instantiate some values in the module loading. For example, we have instantiated the module with person object in the line 9.

Modules are cached in node.js for optimal loading speed. More information can be found on Node.js documentation for Module Caching. The above code may create problem if the values of person objects needs to be different each time the module is loaded. We can prevent this by using the method of Example 4 below.

Example 4:

We can also export a JS function as an object in module. Lets see an example personmod1.js:

var Person = function () { = {
        name: null, 
        age: null ,
        dept: null,
        sal: null
    this.fillData = function(info) {
        for (var prop in {
            if  ([prop]!=undefined) {
      [prop] = info[prop]; 
    this.getSeniority = function() {
        var comment = "junior";
        if ( > 30) {
            comment = "senior"
        return comment;

module.exports = Person;

In the above code we have created a function to emulate an object based on facilities given by Javascript. Here Person will emulate as an object with two functions fillData and getSeniority

We have exported a Person variable with fillData and getSeniority as function. We can use this functions as in following JS file:

var Person = require('./personmod1');

var person  = new Person();

var personInfo = {
        name: "Parth", 
        age: 25 ,
        dept: "Computer",
        sal: "10,000"

person.fillData (personInfo);

console.log (person.getSeniority());