Node.js Modules

Node.js Modules Explained

Node.js Modules is the topic today that we are going to explain in this tutorial. In this tutorial, you will have a deep understanding of Node.js Module.

We have kept this tutorial in multiple parts that will be really useful for you to understand the Node.js Modules in an easy way.

If you don’t have node installed on your system, Please install it first in a easy way.

  1. What are Node.js Modules?
  2. Built-in Node.js Module
  3. External Node.js Module
  4. User defined Node.js Module

So let’s dive into the tutorial and understand What is a Node.js Modules?

What are Node.js Modules?

When we start building Node.js Application, We can write all our code in a single index.js or app.js file.

The Node.js interpreter doesn’t care about how big or how complex your application is. But when you come after a few months to update or extend some functionality. It will be very difficult to understand and very hard to debug the whole code structure.

So, here Node.js Module come in the picture to avoid these difficulty.

Just think of Node.js Modules as jQuery or javascript libraries that contain functions. We include these libraries in the file to work as we expect.

In the same way, We seperate the collection of functions that relates to each other in the different files and name it Node Modules.

We can keep different parts of our code block in different modules and then merge them into an Application.

Built-in Node.js Modules

Node.js built-in modules also called core modules. Node.js has a set of core modules that one can use without any installation. When a Node.js process starts, these built-in modules compile and load automatically. But, before using this you need to include the module first in order to use its functionality.

Let’s see some useful built-in Node.js Modules that can be very useful in an application.

  1. fs – We can use this module to handle the file system like reading a file, writing into a file etc.
  2. http – We can use this module to make Node.js act as an HTTP server.
  3. https – We can use this module to make Node.js act as an HTTPS server.
  4. os – We can use this module to get information about the operation system.
  5. path – To handle the file paths we can use this module.
  6. querystring – To handle URL query strings we can use this module.
  7. url – We can use this module for URL parsing & resolution.

You can find the whole list of Node.js Modules here with detailed information.

External Node.js Modules

The built-in modules come with the Node.js Installation but what, If your application requires something different functionality that is not provided by built-in Node.js Module.

To fulfil this we have the largest library that is NPM (Node Package Manager), Here you can find some useful modules that really save your time a lot and you can use them by just installing it using some few commands that can also found on particular module section.

See the screenshot below to install Lodash (a popular Node.js Module that is known as a modern JavaScript utility library).

Install Lodash Module

You can find the detailed information about installing and using the Lodash module here.

User defined Node.js Modules

Built-in and external modules are not your own instead it is provided by Node.js itself or NPM library. You can also create your own Node.js Module.

See the code snippets below that creates a module customDateFormat which provides a simple helper function that returns custom date format.

file: customDateFormat.js

function customDateFormat(d) {
  var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

  var day = d.getDate();
  var monthIndex = d.getMonth();
  var year = d.getFullYear();

  return monthNames[monthIndex] + ' ' + day + ', ' + year;
}

exports.getcustomdate = function() {
      return customDateFormat(new Date());
};

In the above code file customDateFormat.js has a function customDateFormat and it is exported, means its attributes and functions are open to use in another file.

Now, In your app.js file just import it using require method:-

const customDate = require('./customDateFormat.js');
console.log(customDate.getcustomdate());

Please note that the Node.js Module system allows us to export the functionality that could be used in another file, In the same way, Its allows us to hide the functionality that is not required outside of the module.

This can be achieved by simply not exporting the functionality using exports. It is really important for keeping the code structure well organized and to get that only export only those functionality that is required.

var customDate = require('./customDateFormat.js');
console.log(customDate.customDateFormat(new Date()));

If we use the above code in the app.js file. It will throw an error because we have not exported the function customDateFormat. Instead, we have exported another custom function getcustomdate that is using customDateFormat function.

Please feel free to ask a question in comment section or you can also suggest me to add or correct any thing that you understand well.

Leave a Reply