NodeJS - Modules
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;
I have saved the above file using "Circle.js". Now I am going to use this module inside another file, app.js, and the code is written as follows.
- var circle=require('./Circle.js');
- var obj=circle(7);
- var output=obj.area();
- console.log(output);
Output
Different ways for loading local node.js module
There are lots of ways to load locally created modules.
There are lots of ways to load locally created modules.
- Using absolute path
- var module = require('/<folder_name1>/<folder_name2>/.../module');
- Using relative pathHere I am not giving .js extension so there is no need to add ".js" Node finds .js files if we do not give .js as an extension, it means the following lines will be the same.
- var module = require('./module');
- var module = require('./module');
- var module = require('./module.js');
- Using folder path
You can also use folder path to load modules as follows:But a folder can contain lots of modules and javascript files so node finds index.js file and loads by default. Otherwise we can create package.json where we can define node module name which we want to load by default. We can write package.json like as follows.- var module = require('./folder_name');
- {
- "name" : "module_name",
- "main" : "./folder_path/module_name.js"
- }
Loading third party modules
Third party Node.js module can be downloaded using NPM (node package manager) which you can download locally or globally. To download globally we use the following command.
Third party Node.js module can be downloaded using NPM (node package manager) which you can download locally or globally. To download globally we use the following command.
- npm install -g <module_name>
- npm install --save <module_name>
- var module= require('module_name');
Caching Modules
In Node.js modules are cached when module is loaded the first time. It means if you load the same node module 2 times then node.js does not load that module again it will copy that module from cache. Example is shown below.
In Node.js modules are cached when module is loaded the first time. It means if you load the same node module 2 times then node.js does not load that module again it will copy that module from cache. Example is shown below.
- console.log("Module Loaded Successfully");
- var my_module1=require('./My_Module');
Now I am modifying app.js and writing the following code.
- var my_module1=require('./My_Module');
- var my_module2=require('./My_Module');
This means that module initializes only once. This is very important to know if you are creating any module.
Author
Sourabh Somani
0
56.1k
4.3m