How To Create Nodejs Module Locally: Using Module.Exports

Webnexs
3 min readAug 7, 2020

Looking to enhance your Node.js development skills? Dive into the world to create Nodejs module locally using module.exports. In this guide, we’ll walk you through the process of building and exporting modules in Node.js to streamline your projects. Discover how to leverage module.exports to organize and modularize your code effectively. Let’s get started on empowering your Node.js applications with custom modules!

The local module is a module created locally in your Nodejs application. It can be also called 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 Nodejs Module Locally In Module.Exports

Create Nodejs Module Local Using Module.Exports
Create Nodejs Module Locally In Module.Exports

WRITING SIMPLE MODULE

Let’s create Nodejs module in a simple that logs the information, data, and errors 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 More: Nodejs — Packages and Modules: How to use it

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

Launching Headless Ecommerce In Node.js, Powered By Microservices.
Launching Headless Ecommerce In Node.js, Powered By Microservices.

LOADING A LOCAL MODULE IN NODEJS

LOADING A LOCAL MODULE IN NODEJS
Create Nodejs Modules

To use or create Nodejs module locally, you need to specify the path of the Javascript file. Later load it using the 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 the require() function and the specified path, where it is 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 being exhibited as an object in Log.js using module.exports, the require() function returns a log object.

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

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

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

C:\> node app.jsInfo: Nodejs started

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

FINAL WORDS

Feel free to contact our Nodejs expert to learn more about how to create Nodejs module and its local development process in detail.

--

--