Modules in Node Js

In this tutorial, we are going to learn about modules in node js. We will learn about creating our own modules in node.js and also discover how to use the shipped modules which come with node.js.

What is a Module?

Module in node.js is a file containing a function or set of functions to solve certain queries, the main reason of using and creating modules is to avoid writing extra lines of code and use the repeating functionality over and over again.

Every module is different from each other and hence should not overwrite or hinder other modules functionalities so that they can be used serving their purpose inside a node.js application.

Types of Modules

There are three main types of modules in node.js:

  1. Local Modules
  2. Core Modules
  3. Third Party Modules

Creating Your Own Modules

You can create your own modules by creating them locally in node js by creating javascript files, let’s create a new file as ‘local.js’ and add the following code into it to see the current time and date of your local machine:

function timeanddate() {
  var date = new Date();
  var n = date.toDateString();
  var time = date.toLocaleTimeString();

  console.log('date:', n);
module.exports = timeanddate

In the above code, we have created a function known as timeandate() and added declared a ‘date’ variable to get the new Date() of your local machine. This will store the current date and time from your local machine to the ‘date‘ variable, next we have created two variables ‘n‘ and ‘time‘ that will convert the Date and Time to String objects, displaying information in a descriptive way.

We then display the date and time using the console.log() function and call the timeanddate() function in the end. Lastly, in order to export it as a module we use the module.exports object to specifically export the timeanddate() function only. If you don’t add module.exports object then by default whatever number of functions are available in the local module file will be exported in the file (‘app.js’) where you are exporting the modules.

After creating a javascript file as a module, we will import it in an another file ‘app.js’ where we will use the require function. If local.js file is present in the same directory of app.js then you can define the path of ‘local.js’ as:

var localmodule = require('./local.js');
console.log('Using Local Modules');

Use the (./) notation to fetch the local module file. The result of the above will be:

Hiras-MacBook-Air:~ Hira$ node app.js
date: Thu May 07 2020
time: 5:03:51 AM
Using Local Modules

Importing and Using a Core Module

Node.js comes with in built core modules that are loaded whenever node.js processing starts, but in order to use those modules you need to import them in your application file using the require() function. Some of the in built core modules are:

To use these core modules, you will use the require() function as:

In order to use Node.js core or NPM modules, you first need to import it using require() function. Let’s say you want to import http as your module then you do the following as shown below:

var module = require('http');

As you can see above, this is how we can import the core module ‘http‘ by using the require() function; we also use the variable to store the required module. We will further create a server using the http module by using the following code:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello World!');

After importing the http module, we will use the createServer() function and send two arguments(could be any) req and res to fetch the request first so that the server will return with a response of ‘Hello World‘. Run ‘node file_name.js’ code on your terminal or command prompt:

node firstapp.js

and then go to the port link where the listen() function will be active on port:3200 at http://localhost:3200. You will see the following result:

modules in node js

Using External Modules

Third party modules can be downloaded through the npm. These types of modules are created by the open node js developer community and are available for download at: