An image

Corporate Blogs

Understanding module.exports And exports in Node.js

E-Commerce Businesses Need a Mobile App

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

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

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

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,

Call the date module functions inside the app.js

// app.js

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

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

The logs produce like,

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

Example: 2

Example: 3

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 developersHire 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
Professional Life of an IT Consultant Interview with Valentin Crettaz
Written by
Young Senior Software Developer having 4+years of experience in Web development, skilled in Golang, NodeJS, Ruby, AngularJS, Ruby on Rails & AWS services. With a handful of skills, he keeps thriving through all the Edge of Web development to become a perfect back end developer. Besides, this young techie will always buzz up & surprise us with his beautiful Movie collections.

Leave a Comment

You have an Idea. We have the Solution.

We help business evolve with lates technologies and infrastructures tailored to their needs and market trends.