Node Js

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).

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.

Avinash

Recent Posts

Cut Video in Specific Start & End Time Using Node Js

Some times in our work, we need to cut a video in specific start &…

1 year ago

JavaScript filter() Function | Array.prototype.filter()

If you don't know about JavaScript filter() function then this tutorial is for you. In…

1 year ago

How to Install and Set Up Bower On Windows 10

In this tutorial, we will learn how to install bower on windows 10. But, before…

1 year ago

JavaScript reduce() Method | Array.prototype.reduce()

JavaScript reduce() method tutorial is the topic, we are going to understand today. So what…

1 year ago

Angular HttpClient get & post Request (GET & Post)

In this post, we will understand Angular HttpClient with example. When Angular version 4.3 launched,…

1 year ago

How to Redirect domain according to country IP using PHP| .htaccess

In this tutorial, we will know how to redirect domain according to a country IP…

1 year ago

This website uses cookies.