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.
So let’s dive into the tutorial and understand What is a 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.
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.
You can find the whole list of Node.js Modules here with detailed information.
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.
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.
Easy Guide to Understanding slice() and splice() in JavaScript When you work with lists in…
In this post, we will understand JavaScript Promises and how to use them effectively. Promises…
Some times in our work, we need to cut a video in specific start &…
If you don't know about JavaScript filter() function then this tutorial is for you. In…
In this tutorial, we will learn how to install bower on windows 10. But, before…
JavaScript reduce() method tutorial is the topic, we are going to understand today. So what…
This website uses cookies.