Node.js Creating and Using Modules

  • by
node-js-custom-module-example-001

What is a Module in Node.js?

Modules in Node.js is a set of functions included in a single or multiple javascripts file which can be used throughout an application. There are built-in modules in Node.js and we can create our own custom modules. A function created in module class remains private and usable to that class unless it is exported.

Modules:

  • Set of functions
  • Can be in one or multiples javascript files.
  • Remains private until functions are exposed using exports.
  • Code can be organized by creating multiple modules in one or more folders.

Built-In Modules:

Node.js provides us with number of built-in modules which we can start using in our application directly. Some of the built-in modules are http, https, path, os, querystring, url, util.

Using Built-In modules:

To use a module we must first import it using require(‘module_name’) syntax.

Example:

var os = require('http');

Using ‘http’ module: index.js

var http = require('http');
const portNo = 3000;
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('Hello there. This is a simple example of Node.js module on port no:' + portNo);
}).listen(portNo);

Run this by using command node index on terminal. You can open a new terminal window by pressing Control + Shift + ` keys together.

node-js-built-in-modules-http-example

How to create Modules?

We can create custom modules to create a reusable functionality for entire application. In Node.js once you create a functionality or define anything inside a javascript file, it remains accessible to only to that specific javascript unless you have exported it to be used. Once a function is exported it becomes accessible to all the classes and other javascript file in our app.

Example: Custom-Modules

Create a new folder in your project with name “Modules” and inside this folder add new file with name “myModule.js

node-js-custom-module-example-001

Edit myModule.js like below:

Modules > myModule.js:

const getDate = () => {
var date = new Date();
return date.toLocaleDateString("en-US");
}

const add = (a, b) => {
return a + b;
}

module.exports.customDate = getDate;
module.exports.add = add;

Here, we are creating two custom modules. One for getting Date in custom format and another for adding two given numbers. module.exports.moduleName will help in making these function available outside of this class.

module.exports.customDate = getDate;
module.exports.add = add;

getDate and add will be accessible to all other classes.

Now we will use these modules in our index.js file. Edit the index.js file as below:

index.js:

const myModule = require('./Modules/myModule');

console.log("1+2=" + myModule.add(1, 2));
console.log("Date:" + myModule.customDate());

First we are declaring an object of our module class i.e. myModule. If you are creating custom modules inside seperate folder in your project you have to use ./ to locate your folder. If your class in which you are going to use module is inside a folder then use ../ to navigate to root path. Now run index.js by using command node index.js in a new terminal window.

You can open a new terminal window by pressing Control + Shift + ` keys together.

Output:

node-js-my-module-result

In the next part of this series we will learn more about in-built modules and use it.



  1. Base64Encode.io - Realtime Encode your string to Base64 format.
  2. Base64Decode.io - Realtime Decode Base64 data to plain text.
  3. Md5Hash.io - Realtime Encoding in Md5Hash.