At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Understanding module.exports And exports in Node.js

  • By Reddy Sai
  • April 24, 2020
  • 1267 Views

In this tutorial, we are going to learn about module.exports and exports in the Node.js framework. How to use module.exports in several ways to expose the functions or variables to the outside. Node.js is a runtime environment a.k.a Framework, which is helpful to execute the Javascript outside of the browser.

What is a Module?

A module is a separate program file in Node.js. When creating a module, it can be classified as a grouping of all related functions into a file. For example,
// date.js

getYear = function() {
   return new Date().getFullYear();
}
getMonth = function() {
   return new Date().getMonth();
}

In the above example, we have grouped date-related functions into the one program file and naming it as date.js. We can re-use these functions in the same programming file. If you want to use these functions elsewhere in the application or some other program, you have to export these functions which are required for re-use.

Purpose of a Module

Keeping all functional programs in a single file is really hard to maintain and deal with during the development. Having the code split up into multiple accessible files allows us to have appropriate file names for every file. This way you can easily identify the module and know where to find it at the times of need. Having modules makes it easier to find certain parts of code which makes it more maintainable.

Export a Module

As the word “export” denotes, sending the things to somewhere it is needed. This is also something similar. Here, we have to make our date.js functions as re-usable in other files. Let’s change the date.js program,
// date.js

module.exports = {
  getYear: function() {
     return new Date().getFullYear();
  },
  getMonth: function() {
     return new Date().getMonth();
  }
}

Now, we can use these functions elsewhere in the application or even in some other program.

Also Read

How To Collect, Customize, And Centralize Node.js Logs

Import a module

To import the module, we are using the “require” keyword in the Node.js. require() is a special function defined as part of the CommonJS spec. To include the modules into the file that exists in another file, with help of require() function call to import. The require function reads the javascript file executes it and then returns the module.exports.
Requiring the date.js file into the app.js
// app.js

let date = require("./date");

Now, we can access all functions from the date.js through the date variable.
Here, the assigned value of the date variable is similar to this,

let date = {
  getYear: function() {
     return new Date().getFullYear();
  },
  getMonth: function() {
     return new Date().getMonth();
  }
}

Call the date module functions inside the app.js
// app.js

let date = require("./date");
console.log(date.getYear());
// output will be the current year. Example: 2020
console.log(date.getMonth());
// output will be the current month. Example: 4

With the above examples, the module.exports imported it to call the date functions from the app.js file. And, we will receive the expected values successfully. Now, we can use the exports instead of module.exports to know about the functionality of exports.
Modify the date.js file like,
// date.js

exports.getYear = function getYear() {
   return new Date().getFullYear();
}
exports.getMonth = function getMonth() {
   return new Date().getMonth();
}

Now, run the app.js file.
app.js file also will give the same output. Then, what is the difference?

Exports vs module.exports

The module is a plain Javascript object with an exports property. module refers to the object representing the current module. This holds the metadata about the module such as filename of the current module and export object.
Log the value of the module and module.exports in the date.js file and execute the date.js.
// date.js

exports.getYear = function getYear() {
  return new Date().getFullYear();
}
exports.getMonth = function getMonth() {
  return new Date().getMonth();
}
console.log('module object: \n', module)
console.log('module.exports object: \n', module.exports)

The logs produce like,

module object:
Module {
  id: '.',
  exports: { getYear: [Function: getYear], getMonth: [Function: getMonth] },
  parent: null,
  filename: '/home/reddy/Desktop/node-practice/date.js',
  ...
}
module.exports object:
{ getYear: [Function: getYear], getMonth: [Function: getMonth] }

The module object exports key-value is as same as the module.exports values.

Export modules in multiple ways

As we know, to export functions and variables in the object format to use these somewhere in the application. There are several ways to export modules. Here are some examples,
Example: 1

getYear = function() {
    return new Date().getFullYear();
}
getMonth = function() {
    return new Date().getMonth();
}
module.exports = {getYear, getMonth};

Example: 2

const date = {
  getYear: function() {
     return new Date().getFullYear();
  },
  getMonth: function() {
     return new Date().getMonth();
  }
}
module.exports = date;

Example: 3

const getYear = function() {
  return new Date().getFullYear();
}
const getMonth = function() {
  return new Date().getMonth();
}
module.exports.getYear = getYear;
module.exports.getMonth = getMonth;

Other useful things to know

When the require() function is called to the same module in multiple files, the module has to be loaded once only. Next time a require() function is called on the same module then pulls from the cache.
The require() function loads modules in synchronously. Suppose, it loads modules in asynchronously, we could not access the objects in the right way. So, the synchronous behavior is required while requiring modules one file to another.
I hope you have found this useful. You can start to test this code snippet to get a clear understanding of this concept.
Share with your Node.js folks out there! Feel free to comment on your doubts and suggestions in the comment section.
Looking for highly skilled JS developers? Hire Node.js developers at Agira. Get started with your dream project right with our expertise. Get in touch with us today!

Turn your vision to magnificent reality With
Our Web and Mobile Solutions