This article will explain Node.js and its modules with examples, and we will create our own custom server using the Node.js application.
To understand and create the Node.js application, please download and install it from the official site: node.js
Node.js
According to official documentation,
- Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine for easily building fast and scalable network applications.
- Node.js is free
- Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
- Node.js uses JavaScript on the server
- Node.js modules
Module in Node.js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node. js application.
In short we can break large javascript files into multiple files and reuse it.
Let's create your first project in node.js application
Hello World
Step 1
Open command prompt and navigate to your project directory (My project directory is D:\Arvind\solution\nodesolution\nodeExamples).
Step 2
Type npm init and answer the following question as per the following details.

Step 3
To create a blank .js file type the below command (It will create blank index.js in our current directory).
- type Nul > index.js
Step 4
Open index.js file in your editor (visual studio code) and write the below code.
- console.log("Hello World")
Step 5
To run and see the ouput type node index.
- node index

Since we had completed our first project in node.js application, let's see the node modules.
Node.js Module Types
There are basically three types of modules,
- File based Modules
- Core Modules
- External or Third Party Modules
File based Modules
This types of modules are created locally in your node.js application. It includes different functionalities of your application in seperate files and folders.
The require function is used to import a module in nodes.js application.
Example
Create LocalModule.js file and insert the follwing code.
- // create a variable calc that have four function add, subtract, multiply and divide.
- const calc = {
- // Add the two number
- add: function (num1, num2) {
- return num1 + num2
- },
- // Subtract the two number
- subtract: function (num1, num2) {
- return num1 - num2;
- },
- // multiply the two number
- multiply: function (num1, num2) {
- return num1 * num2;
- },
- // divide the two number
- divide: function (num1, num2) {
- return num1 / num2
- }
- };
- // export the modules to consume on different modules.
- module.exports.calc = calc;
To use the above functionality in a different module, create index.js and write the code
- // To use custom module declare the code like this.
- const myCalculation = require('./LocalModule.js');
- // Declare two variable
- const num1 = 5;
- const num2 = 4;
- // consume the existing function from LocalModule.js
- console.log(`Addition of ${num1} and ${num2} is: ${myCalculation.calc.add(num1, num2)}`);
- console.log(`Subtraction of ${num1} and ${num2} is: ` + myCalculation.calc.subtract(num1, num2));
- console.log(`Multiply of ${num1} and ${num2} is: ` + myCalculation.calc.multiply(num1, num2));
- console.log(`Divide of ${num1} and ${num2} is: ` + myCalculation.calc.divide(num1, num2));
Now type node LocalModule to see the ouput.

Core Modules
It is also known as (built in modules).
These core modules are compiled into its binary distribution and load automatically when Node.js process starts.
In order to use Node.js core or NPM modules, you first need to import it using require() function as shown below.
- const variableName = require('module_name');
- const path = require('path');
Example
Write the below code in CoreModule.js file.
- // declare the core library file.
- const path = require('path');
- //Use the core library file.
- console.log("Directory of index file: " + path.dirname(__filename));
- console.log("Extension of index file: " + path.extname(__filename));
- // check more detail on path library from here https://nodejs.org/dist/latest-v12.x/docs/api/path.html
- // For rest of core libary https://nodejs.org/dist/latest-v12.x/docs/api/
To see the ouput type command node CoreModule

External or Third Party Module


Onkar SharmaPosted Apr 24, 2020, 2:09 AM
Nice one...
Shailesh ChavdaPosted Apr 24, 2020, 12:55 AM
Dear Arvind, really helpfull to me to regarding Node.js article.