What is module in Node.js?
A set of functions you include in your Node.js application, that is a module. Modules can be either single files or directories containing one or more files. Let's see in detail
Node.js Modules
Consider modules to be the same as JavaScript libraries. A module encapsulates code into a single unit of code.
Whenever you create any type of functions/objects in Node.js and you reuse these functions/objects, then this process you can call a module.
Note:
Whenever if we don't create any Node.js modules ourselves, then we already have modules at our disposal because the Node.js environment provides so many built-in modules for us.
# Roles inside of Node.js
The Module has two main roles inside of Node.js.
1. Foundation Role
The module's first role is to provide a foundation for all Node.js modules to build off. Each file is given a new example of this base module on load, which persists even after the file has run. This's why we are able to attach properties to "module.exports" and return them later as needed.
2. Loading Mechanism
The module's second big job is to handle Node.js module loading mechanism. The require() function that we use is actually an abstraction over module.require, which is itself just a simple wrapper around Module._load. This load method handles the actual loading of each file, and where we'll begin our journey.
1. Built-in Modules
Node.js has a set of built-in modules which you can use without any further installation. Built-in Modules are provided by the system.
-
•
Built-in modules are also called Core modules.
-
•
The Built-in modules are defined within Node.js source and are located in the "lib/" folder.
-
•
The Built-in modules have several modules compiled into the binary and load automatically when Node.js process starts.
-
•
Built-in modules are always preferentially loaded if their identifier is passed to require(). For example, require('http') will always return the built in HTTP module, even if there is a file by that name.
How to Include Built-in Modules
The module is implemented in the require('module') function, by using require() function you include the Built-in module in Node.js
→ Example: Include the "http" built-in module
var http = require('http');
Now your Node.js application has included and access to the HTTP module, and is able to create a server:
→ Example: Include and Use http Module
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js!');
}).listen(8080);
2. Custom Modules
Custom modules are created by user, by using exports keyword and use it in your application.
-
•
It is also called User defined modules, File modules or Own modules.
-
•
With the help of the exports keyword to make properties and methods available outside the module file.
How to create your Custom modules
You use exports keyword to create the Custom module in Node.js
→ Example: mymodule-file.js
Create a module that returns the value of add() function
exports.add = function(x, y) {
return x + y;
}
How to Include Custom modules
require('module') function also used to include Custom module in Node.js
→ Example: index-file.js
Use the module "mymodule-file.js" in a Node.js file
var http = require('http');
var module_object = require('./mymodule-file'); // Here included mymodule-file.js
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("mymodule-file output is : " + module_object.add(25, 60).toString());
res.end();
}).listen(8080);
Summary
You've learned about Node.js modules - both built-in modules provided by the system and custom modules you can create using the exports keyword. Modules help organize and reuse code effectively in Node.js applications.