Node.js Modules

Node.js Modules: At the point when we'll begin composing Node.js applications, we would place all our code into one big node.js file, regardless of how enormous or complex our application is. As individual, the user should think about how to structure the code, which is why Modules are introduced.

Module in Node.js is a simple or intricate functionality structured in solitary or numerous JavaScript files that can be reused during the Node.js application.

Every module in Node.js has its environment not to pollute the global scope. Moreover, we can save each module in different .js files in different folders.

There are 3-Types of Modules available in Node.js, which are as follows:

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

Let’s discuss each module in detail.

Core Modules in Node.js

The core modules incorporate the absolute minimum functionalities of Node.js. These core modules are aggregated into its double appropriation and load naturally when the Node.js process begins. However, it is necessary to import the core modules before using them in Applications.

Node.js has a collection of core modules that are a segment of the platform. Some of them are as follows:

NameDescription
httpIt is comprised of classes, methods and events that are required to create a Node.js http server.
urlIt contains the procedures for URL resolution and parsing.
querystringIt contains the systems related to the query string.
pathIt contains the procedures to deal with file paths.
fsIt contains classes, methods, and events to function with the file I/O.

Executing Core Modules

To execute Node.js core or NPM modules, we have to import it by calling the require() function as shown in the following syntax:

var module = require('module_name');

As we can observe from the above syntax, we have specified the module name through the require() function. This will return an object, function, property, or other JavaScript types, relying on the stated module returns.

Here's a good example demonstrating the above statement.

/*The require() function is used to call the required module */
var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('Hey there! Welcome to Node.js Tutorials');
  }).listen(8080);

The Output for above syntax will be:

Hey there! Welcome to Node.js Tutorials.

Local Modules in Node.js

Local modules are the modules formed locally in our Node.js application. 

Our application's various functionalities are contained in these modules, in different files and folders, and can be used to complete any present task. 

Creating our Own Module

Let us create our own Node.js Module, by creating a Node.js file. We’ll call it as 'local.js'

function getRandom(min, max) {
    return Math.random() * (max - min) + min;
}
module.exports.btw1and20 = function() {
    return getRandom(1, 20);
};

In the above example, we have created a function – getRandom(). In the end, we have assigned this function to the module.exports variable. The module.exports in the above example displays the function.

Loading a Local Module

To utilize local modules in our application, we have to load it by utilizing the require() function similarly to the core module. However, we have to indicate the path of the .js file containing the module.

Here's the demonstration to understand the working of a Local Module.

We've created another Node.js file, and saved it as 'getRandom.js'

var myRandom = require('./local.js');
console.log(myRandom.btw1and20());

Here, we'll use the require function to call the local myRandom module's exported attributes available - in this case, the btw1and20 function.

Thus, we can form our modules using module.exports and utilize it in our applications.

Third-Party Modules in Node.js

The third-party module can be downloaded by NPM (Node Package Manager). Other users create these kinds of modules, and we can utilize them in our projects. The Build-in modules allow us to tackle many coding issues without wasting time for each new application; however, what truly helps Node.js programming efficiency is the immense environment of open source modules given by the Node.js community. The best third-party module models are probably recorded as follows: gulp, express, async, socket.io, lodash, mongoose, pm2, bower, underscore, q, debug, react, and many more.

Third-party modules can be introduced inside the venture envelope or globally.

To download globally, we use the following command.

npm install -g <module_name>  

Here we use -g to install package globally.

If we want to install the modules locally then we can use the following command.

npm install --save <module_name>  

The above command will download node package inside the node_modules folder, and then we can directly use the require() function to call node module. 

var module= require('module_name');