×

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');   

Related Topics

Node.js Tutorial

Introduction to Node.js: Node.js is a non-proprietary and platform-independent runtime environment for implementing JavaScript code outside of a browser. However, NodeJS isn't a framework and not a programming language. We...

4 minutes read.

Node.js Event Loop Phases

Node.js – Phases of Event Loop Once we have understood the Node.js event loop concept and the basic introduction to its phases, let's discuss the functions of the Check handler phase...

4 minutes read.

Node.js Event Loop

An Introduction to Event Loop As we have already discussed, Node.js is an Asynchronous platform with a non-blocking I/O. Node.js has awesome features that made it this thriving, and the Event...

5 minutes read.

Node.js Events Module

Introduction to the Node.js events Module As we’ve already discussed, Node.js is a JavaScript runtime with an event-driven architecture designed to perform Asynchronous tasks. In order to accomplish the event-driven architecture,...

4 minutes read.

Node.js Console

Node.js Console: Like the JavaScript console mechanism, a console module is designed for Node.js that provides a debugging console for writing or printing messages. Usually, in JavaScript, a console mechanism is...

4 minutes read.

Node.js OS Module

Introduction to Node.js OS Module The OS Module in Node.js provides the Operating System-related Utilities. This module delivers the functions to interact with the Operating System. It also delivers the Operating...

5 minutes read.

Node.js Callback Concept

Understanding the Concept of Callback A Callback is an asynchronous equivalent for a function that is called at the completion of a task. The Callback helps to avoid any I/O blocking and allows other code to execute...

2 minutes read.

Difference Between Node.js and PHP

Node.js vs PHP Node.js and PHP both are the powerful back-ends to create versatile websites. Even when these two technologies fall under the same category, their features are pretty diverse. There's...

6 minutes read.

Node.js Error Handling

Node.js – Error Handling: Errors are part of every programming language, and Node.js Environment is not an exception. There are many predefined errors in Node.js, including errors like SyntaxError, RangeError, and...

4 minutes read.

How to Install Node.js on Windows?

Environment Setup for Node.js Since Node.js is a mandatory section of the program that runs JavaScript, similar to all other required section of programs that run a programming language, it is...

4 minutes read.

Node.js File System

Introduction to File System in Node.js FS, abbreviated as File System, is a Node.js Build-in Module, which provides an Application Programming Interface (API) to interact with the File System, and to...

15 minutes read.

Node.js Path Module

Understanding the path Module in Node.js The path module is a built-in module of Node.js that provides various methods to interact with the files and directory path names on the device’s...

6 minutes read.

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

4 minutes read.

Node.js HTTP Module

An Introduction to HTTP Module The http module is a core module in Node.js that uses the Hypertext Transfer Protocol (HTTP) to transfer data. With the help of the http module,...

7 minutes read.

Node.js REPL

Node.js REPL: REPL represents the Read Eval Print Loop. REPL is modest; much more, an interactive software development environment similar to the command line shells used in various Operating Systems....

4 minutes read.

Node.js Core Debugger

Node.js – Core Debugger: It’s always a challenge when it comes to debug an application. The Asynchronous workflows of Node.js include an additional layer of intricacy to this difficult process....

3 minutes read.

Difference Between Nodejs Angular and React

A Right JavaScript Framework Within a business, it is necessary to pick the right technology. As much as this applies to the web application and software development, it also applies in...

5 minutes read.

Node.js Parsing Arguments

Understanding Command-Line Arguments The Strings of text used to transfer extra information to a program through the command-line interface (CLI) while execution of the application, is known as Command-Line arguments. These...

9 minutes read.

Difference Between Node.js and Django

Node.js vs Django In Web Application Development, we must have a robust technology to provide strength to our Web application. Node.js and Django both are pretty commanding technologies that help us...

6 minutes read.

Node.js Buffers

An Introduction to Buffers A Buffer is an area in memory (typically RAM) that is used to store binary data. Binary is a collection or a set of 0 and 1....

4 minutes read.