Introduction
In my previous article I gave an Introduction about NodeJS. In this article I will explain modules in NodeJS, loading modules, creating modules and about node_modules folder.
In my previous article I gave an Introduction about NodeJS. In this article I will explain modules in NodeJS, loading modules, creating modules and about node_modules folder.
Node.js has a simple module loading system. A module in node.js is a simple or complex functionality organized in single or multiple javascript files which can be reusable again.
Node.js Module Types
In Node.js modules can be categorized in 3 types https://nodejs.org/api/modules.html#modules_core_modules,
In Node.js modules can be categorized in 3 types https://nodejs.org/api/modules.html#modules_core_modules,
- Core Modules
- Local Modules
- Third Party Modules
Node.js Core Modules
Node.js has several modules compiled into its binary distribution, and load automatically when the Node.js process starts, these are called the core modules. These core modules of node.js are located within Node.js's source and are locate d inside "lib" folder. Some of the core modules are listed below.
Node.js has several modules compiled into its binary distribution, and load automatically when the Node.js process starts, these are called the core modules. These core modules of node.js are located within Node.js's source and are locate d inside "lib" folder. Some of the core modules are listed below.
- http - This module is used to create http server.
- fs - This module is used to perform file operations like reading, writing, appending and deleting files etc.
- Crypto - This module provides cryptographic functionalities like encryption, decryption, sign, verification, digesting etc.
- Querystring - This method includes methods to deal with querystring like unescapeBuffer, unescape, escape, encode, stringify, decode and parse.
- url - This module includes methods for url resolutions,resolve, parsing, format etc.
- path - This module is used to deal with file paths when working with file system.
Local Modules
Local modules are user defined modules which are mainly used for specific projects and locally available in separate files or folders within project folders. These type of modules contain application specific functionality.
Note
We can package locally created local modules and distribute them via NPM (Node Package Manager), which can be used by others and the node community.
Local modules are user defined modules which are mainly used for specific projects and locally available in separate files or folders within project folders. These type of modules contain application specific functionality.
Note
We can package locally created local modules and distribute them via NPM (Node Package Manager), which can be used by others and the node community.
Third party module
The third party module can be downloaded by NPM (Node Package Manager). These type of modules are developed by others and we can use that in our project. Some of the best third party module examples are listed as follows: express, gulp, lodash, async, socket.io, mongoose, underscore, pm2, bower, q, debug, react, mocha etc.
Third party modules can be install inside the project folder or globally.
The third party module can be downloaded by NPM (Node Package Manager). These type of modules are developed by others and we can use that in our project. Some of the best third party module examples are listed as follows: express, gulp, lodash, async, socket.io, mongoose, underscore, pm2, bower, q, debug, react, mocha etc.
Third party modules can be install inside the project folder or globally.
How to load a module?
To load a module in your node application you can just use "require()" function. whose syntax is given below.
To load a module in your node application you can just use "require()" function. whose syntax is given below.
- var module=require('module_name');
Loading core module
Core modules can be loaded as follows.
Core modules can be loaded as follows.
- var http=require('http');
How to create and load local module?
In Node.js files and modules are in a one-to-one correspondence. The following example will explain to you how to create a local Node.js module.
In Node.js files and modules are in a one-to-one correspondence. The following example will explain to you how to create a local Node.js module.
- function Circle(radius) {
- return {
- area: function area() {
- return Math.PI * Math.PI * radius;
- }
- };
- }
- module.exports = Circle;



Join the conversation! Your thoughts help the community grow.