A set of functions you include in your node.js application, that is a module. Modules can be either be single files or directories containing one or more file. Let's see in detail
Node.js Modules
Consider modules to be the same as Java-Script 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 the these functions/objects , then these process you can called module.
For example : Mathematical calculation, Fatch the data from database and any type of task you want to perform , then you need create a module.
Type of modules in node.js.
Built-in Modules
Custom modules (User defined modules).
Note : When ever 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.
The module’s First, it provides 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 attach properties to "module.exports" and return them later as needed.
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 is provided by the system.
Built-in modules is 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 has 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/plan'}); res.end('Hello Node.js!'); }).listen(8080);
2. Custom modules
Custom modules is 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/plan'}); res.write("mymodule-file output is : "+module_object.add(25,60).toString()); res.end(); }).listen(8080);
Comments