The module in Node.js is a easy or complex application organized into one or more JavaScript files which can be reused all over the Node application.

Each module in Node.js has its own format, so it cannot interfere with other modules or pollute the earth's surface. Even, every module can be placed in a separate .js file under a separate folder.

Node.js uses standard CommonJS modules. CommonJS is a group of volunteers who define JavaScript standards for web server, desktop, and console application.

Types of Node.js Module

Node.js includes three types of modules:

1) Core Modules

2) Local Modules

3) Third Party Modules

Node.js Core Modules

Node.js is a simple weight frame. Basic modules include a small empty Node.js application. These basic modules are integrated into their binary distribution and are automatically loaded when the Node.js process begins. However, you need to import the main module first in order to use it in your application.

Loading Core Modules

In order to use Node.js core or NPM modules, you first requirement to import it using require() function as shown down.

var module = require('module_name');

According to the syntax above, specify the name of the module in operation of the require(). The requirement() function will retrieve an object, function, property or other form of JavaScript, depending on the module specified.

The following example shows how to use the Node.js http module to build a web server.

Example

var http = require('http');

var server = http.createServer(function(req, res){

  //write code here

});

server.listen(5000);

In the instance above, the require() function returns the object because the http module returns its functionality as an object, you can use its assets and methods using dot notation e.g. http.createServer ().

This way, you can download and use Node.js modules in your app. We will use basic modules in all of these lessons.

Learn about local modules in the next section.