How to create a local module in Nodejs using module.exports?

By Haribabu | Posted on February 5, 2020

Let dig deep about how to create a local module in Nodejs using module.export in this blog.

The local module is a module created locally in your Nodejs application. It can be also called as Custom or User-defined modules. In other words, Local modules are mainly used for specific projects that are locally available within the project folders. So these modules contain different functionalities of your application that are separately available in files and folders.

Through the Node package manager(NPM), you can create, package, and submit it to the Nodejs community. So that they can use it for their Nodejs projects. For example, these modules allow you to connect and fetch data from MongoDB, which can be reused in your application.

create a local module in Nodejs using module.exports

Create local module in Nodejs

WRITING SIMPLE MODULE

Let’s create a simple module that logs the information, data, error to the console.

In Nodejs, it is mandatory to place a module in a separate JavaScript file. So, just create a Log.js file and render the following code: 

log.js

var log = { 

info: function (info) { 

console.log('Info: ' + info); 

}, 

warning:function (warning) { 

console.log('Warning: ' + warning); 

}, 

error:function (error) { 

console.log('Error: ' + error); 



};

 module.exports = log

For your reference, we have created an object with three main functions in the above logging module example. Those functions include – info(), warning() and error(). 

In the above example, the module.export exhibits a log object as a module as we assigned this object to module exports.

Module.export is a default special object in every Nodejs application. You can use this module.exports or exports to exhibit a function, object, or variable as a module in Nodejs.

Read: Nodejs – Packages and Modules: How to use it

Let’s see how to deploy this logging module in your application below,

LOADING A LOCAL MODULE in Nodejs

node-modules

To use or create a local module in Nodejs, you need to specify the path of the Javascript file. Later load it using require() function as same as in the core module. Thus, we illustrate how to use the above logging module contained in Log.js.

app.js

var myLogModule = require('./Log.js'); 

myLogModule.info('Nodejs started');

Follow the below steps to get to know about the process of using the logging module, 

  • It loads the logging module using require() function and specified path, where it stored.
  • Within the root folder, the logging module is present in the Log.js file.
  • Specify the path ‘./Log.js’ in the require() function, where ‘ denotes a root folder.

Due to the logging module exhibits as an object in Log.js using module.exports, the require() function returns a log object.

So now you can use logging module as an object and call any of its function using dot notation as follows,

  • myLogModule.info()
  • myLogModule.warning
  • myLogModule.error()

Now, run the above illustration using the command prompt in a Windows as shown below,

C:\> node app.js

Info: Nodejs started

Thus, in this way you can create a local module in Nodejs using the module.exports. Secondly deploy that in your application in your future projects.

FINAL WORDS

Feel free to contact our Nodejs expert to know more about the Nodejs development process in detail.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *