Node JS
June 3, 2026 7 min read

Comprehensive Guide to Node.js: Features, Modules, and NPM

HS
HEMANT SINGH
Technical Writer & Educator

Comprehensive Guide to Node.js: Features, Modules, and NPM

Aman

Technical Writer & Educator

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment.

It allows JavaScript to run outside the browser, especially on the server side.

Node.js uses Google's V8 JavaScript engine to execute JavaScript code.

With Node.js, JavaScript can be used for both:

  1. Client-side development
  2. Server-side development

Features of Node.js

1. Event-Driven

Node.js uses an event-driven architecture. It can respond to events such as requests, file operations, and user actions.

2. Non-Blocking

Node.js does not wait for one operation to finish before handling other tasks.

3. Asynchronous I/O

Node.js can perform input/output operations asynchronously, allowing other tasks to continue while waiting for operations such as file or database access.

Uses of Node.js

Node.js is commonly used for:

  1. Web servers
  2. REST APIs
  3. Real-time applications
  4. Chat applications
  5. Backend development
  6. Command-line tools

Installing Node.js

Download and install Node.js from the official Node.js website.

After installation, check the version:

node -v

Check NPM:

npm -v

HTTP Module

The built-in http module can be used to create an HTTP server.

It does not require an additional package.

const http = require("http");

const server = http.createServer((req, res) => {

res.writeHead(200, {
"Content-Type": "text/plain"
});

res.write("Hello from Node.js");

res.end();
});

server.listen(3000);

Save the file as:

first.js

Run it using:

node first.js

HTTP Response Methods

writeHead()

writeHead() sends the HTTP status code and response headers.

res.writeHead(200, {
"Content-Type": "text/plain"
});

200 means the request was successful.

Content-Type: text/plain tells the browser that the response contains plain text.

write()

write() sends data to the response body.

res.write("Hello World");

end()

end() tells Node.js that the response has been completed.

res.end();

You should normally call end() after writing the response.

Port Environment Variable

The port can be provided using an environment variable.

process.env.PORT

A common approach is:

server.listen(process.env.PORT || 3000);

Here:

  1. process.env.PORT is used if available.
  2. 3000 is used as the default port.

Node.js Routing

Node.js can check the requested URL using:

req.url

Example:

const http = require("http");

const server = http.createServer((req, res) => {

if (req.url === "/") {
res.end("Home Page");
}
else if (req.url === "/about") {
res.end("About Page");
}
else {
res.end("Page Not Found");
}

});

server.listen(3000);

Here, req.url tells us which URL the client requested.

Nodemon

Normally, when you change your Node.js code, you need to restart the application manually.

Nodemon automatically restarts the application when files change.

Install Nodemon

npm install -g nodemon

Run the Application

nodemon first.js

Now the application automatically restarts when you modify the file.

Environment Variables with dotenv

Environment variables are useful for storing configuration values.

Create a .env file in the project root:

WEBSITE_NAME=My Website
PORT=3000

Install the dotenv package:

npm install dotenv

Load the .env file:

require("dotenv").config();

Access an environment variable:

console.log(process.env.WEBSITE_NAME);

Output:

My Website

Console Output in Node.js

Node.js provides the console object for displaying information in the terminal.

Format Specifiers

%s

Used for strings.

console.log("Name: %s", "Aman");

%d

Used for numbers.

console.log("Age: %d", 25);

%i

Used for integers.

console.log("Number: %i", 25.8);

%o

Used for objects.

console.log("Student: %o", {
name: "Aman",
age: 25
});

Console Methods

console.log()

Used to print information.

console.log("Hello World");

console.clear()

Clears the console.

console.clear();

Import and Export in Node.js

Node.js supports the CommonJS module system using require() and module.exports.

Exporting a Value

Create a file named:

student.js
let name = "Aman";

module.exports = name;

Import it into another file:

let name = require("./student");

console.log(name);

Output:

Aman

Exporting Multiple Values

You can export values as properties.

let name = "Aman";
let age = 25;

exports.name = name;
exports.age = age;

Import them:

let { name, age } = require("./student");

console.log(name);
console.log(age);

Output:

Aman
25

NPM

NPM stands for Node Package Manager.

It is used to:

  1. Install packages
  2. Remove packages
  3. Update packages
  4. Manage project dependencies
  5. Run project scripts

Installing a Package

npm install package_name

Example:

npm install express

The package will be added to the project's dependencies.

Updating Packages

To update installed packages:

npm update

Uninstalling Packages

Remove a local package:

npm uninstall package_name

Remove a globally installed package:

npm uninstall -g package_name

package.json

package.json contains information about a Node.js project and its packages.

A basic package.json may look like:

{
"name": "my-app",
"version": "1.0.0",
"description": "My Node.js application",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {},
"devDependencies": {}
}

Important Properties

name

The name of the application.

version

The current version of the application.

description

A short description of the application.

main

The main entry point of the application.

scripts

Defines commands that can be run using NPM.

dependencies

Contains packages required by the application at runtime.

devDependencies

Contains packages mainly required during development.

NPM Scripts

Example:

{
"scripts": {
"start": "node app.js"
}
}

Run the script using:

npm run start

For the start script, you can also use:

npm start

Installing a Development Dependency

Use:

npm install package_name --save-dev

Example:

npm install nodemon --save-dev

The package will be added to devDependencies.

OS Module

The built-in os module provides information about the operating system.

Import it:

const os = require("os");

Common Methods

os.arch()

Returns the CPU architecture.

console.log(os.arch());

Example output:

x64

os.cpus()

Returns information about the system CPUs.

console.log(os.cpus());

os.freemem()

Returns the amount of free system memory in bytes.

console.log(os.freemem());

os.homedir()

Returns the current user's home directory.

console.log(os.homedir());

os.networkInterfaces()

Returns information about the available network interfaces.

console.log(os.networkInterfaces());

os.platform()

Returns the operating system platform.

console.log(os.platform());

os.totalmem()

Returns the total system memory in bytes.

console.log(os.totalmem());

os.type()

Returns the operating system name.

console.log(os.type());

os.uptime()

Returns the number of seconds the system has been running since the last restart.

console.log(os.uptime());

os.userInfo()

Returns information about the current user.

console.log(os.userInfo());

Path Module

The built-in path module provides utilities for working with file and directory paths.

Import it:

const path = require("path");

Common Methods

path.basename()

Returns the last part of a path.

console.log(path.basename("/home/user/app.js"));

Output:

app.js

path.dirname()

Returns the directory part of a path.

console.log(path.dirname("/home/user/app.js"));

Output:

/home/user

path.extname()

Returns the extension of a file.

console.log(path.extname("app.js"));

Output:

.js

path.join()

Joins multiple path parts together.

let filePath = path.join("users", "aman", "app.js");

console.log(filePath);

File System Module

The built-in fs module is used to work with files and directories.

Import it:

const fs = require("fs");

fs.readFile()

Reads the content of a file asynchronously.

fs.readFile("data.txt", "utf8", (err, data) => {

if (err) {
console.log(err);
return;
}

console.log(data);
});

fs.writeFile()

Writes data to a file.

If the file does not exist, it can create the file.

fs.writeFile("data.txt", "Hello World", (err) => {

if (err) {
console.log(err);
return;
}

console.log("File written successfully");
});

fs.appendFile()

Adds new content to the end of a file.

fs.appendFile("data.txt", "\nHello Again", (err) => {

if (err) {
console.log(err);
return;
}

console.log("Data added");
});

fs.unlink()

Deletes a file.

fs.unlink("data.txt", (err) => {

if (err) {
console.log(err);
return;
}

console.log("File deleted");
});

fs.rename()

Renames a file or directory.

fs.rename("old.txt", "new.txt", (err) => {

if (err) {
console.log(err);
return;
}

console.log("Renamed successfully");
});

fs.mkdir()

Creates a new directory.

fs.mkdir("uploads", (err) => {

if (err) {
console.log(err);
return;
}

console.log("Directory created");
});

fs.rmdir()

Removes a directory.

fs.rmdir("uploads", (err) => {

if (err) {
console.log(err);
return;
}

console.log("Directory removed");
});

Note: For newer Node.js code, fs.rm() is generally preferred for removing directories because fs.rmdir() is deprecated in current Node.js versions.

Quick Revision

Node.js

JavaScript Runtime Environment
↓
Runs JavaScript outside the browser
↓
Uses Google's V8 Engine

Important Built-in Modules

http → Create HTTP servers
os → Operating system information
path → Work with file paths
fs → Work with files and directories

NPM

npm install → Install packages
npm update → Update packages
npm uninstall → Remove packages
npm run → Run scripts

Common Modules

HTTP Module
createServer()

OS Module
arch()
cpus()
freemem()
homedir()
networkInterfaces()
platform()
totalmem()
type()
uptime()
userInfo()

Path Module
basename()
dirname()
extname()
join()

File System Module
readFile()
writeFile()
appendFile()
unlink()
rename()
mkdir()
rmdir()