Updated on 02 Jun, 202617 mins read 992 views

Without any further ado, let's get our hand dirty with the Node.js.

Installation

Step 1: Download Node.js

Visit the official Node.js website:

Node.js Official Website

Download the LTS (Long-Term Support) version, which is recommended for most users.

Step 2: Verify Installation

Open a terminal or command prompt and run:

node -v
OR
node --version

Both are same.

Example output:

v24.16.0

Check npm (Node Package Manager):

npm -v
OR
npm --version

Both works.

Example output:

11.13.0

Understanding the Node.js Distribution

When you install Node.js, you actually receive several important tools.

The installation package includes:

Node.js Runtime
npm (Node Package Manager)
Core Node Modules
Development Tools

Let's understand each component.

Node.js Runtime:

The runtime executes JavaScript code outside the browser.

Example:

console.log("Hi");

Without Node.js:

Runs inside browser only

With Node.js

Runs directly on your computer

npm (Node Package Manager)

npm is the world's largest software package registry.

It allows developers to:

  • Install libraries
  • Manage dependencies
  • Share packages
  • Automate development tasks

Example:

npm install express

This command downloads and installs the Express framework.

Today, millions of packages are available through npm.

Your First Node.js Program

Create a file named:

app.js:

Add the following code:

console.log("Hello, Node.js");

Run it using:

node app.js

Output:

Hello, Node.js

Yippeee, you just created your first Node.js program.

Understanding npm

npm (Node Package Manager) comes installed with Node.js and allows you to install libraries and tools.

Initialize a new project:

mkdir my-node-app
cd my-node-app
npm init -y

This creates a file named:

package.json

The package.json file stores project information and dependencies.

Installing Packages

Install a package using npm.

Example:

npm install lodash

This downloads the package and adds it to:

node_modules/

You can now use it in your code:

const __ = require("lodash");
console.log(__.capitalize("welcome to nodejs"));

Output:

Welcome to nodejs

Working with Modules

Node.js uses modules to organize code.

math.js:

function add(a, b) {
	return a + b;
}

module.exports = add;

app.js:

const add = require("./math");
console.log(add(5, 3));

Output:

8

Modules help keep applications clean and maintainable.

Node Version Manager (NVM)

As a developer, you will often work on multiple projects.

Project A: Node 20
Project B: Node 22

Installing and uninstalling versions repeatedly becomes difficult.

This problem is solved using NVM.

What is NVM?

NVM stands for:

Node Version Manager.

It allows you to:

  • Install multiple Node versions
  • Switch between versions
  • Test compatibility

Example:

nvm install 20
nvm install 22

Switch:

nvm use 22

Check:

node -v

Output:

v22.x.x

Create a Simple HTTP Server:

One of Node.js's most common uses is building web servers.

Save the following code in a file named app.js:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello, Node.js!\n');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Run the Application:

Open the terminal, navigate to your project directory, and execute:

node app.js

View in Browser:

Open your browser and visit: http://localhost:3000 to see the Node.js application in action.

Understanding Asynchronous Programming

Node.js is famous for its asynchronous nature.

Example:

console.log("Start");

setTimeout(() => {
	console.log("Processing..");
}, 2000);

console.log("End");

Output:

Start
End
Processing

Understanding the Node.js Runtime

When you run:

node app.js

Node.js:

  1. Reads the JavaScript file.
  2. Executes the code using the V8 engine.
  3. Code compiles to machine code.
  4. Runtime executes instructions.
  5. Provides additional APIs such as:
    1. File System
    2. HTTP Server
    3. Networking
    4. Process Management
    5. Streams
  6. Output appears

These APIs are not part of JavaScript itself but are provided by Node.js.

REPL (Read-Eval-Print Loop)

Node.js includes an interactive environment called REPL.

Start it:

node

Example:

> 5 + 10
15

> "Hello".toUpperCase()
HELLO

REPL is useful for testing small code snippets and learning JavaScript concepts.

Modules in Node.js

Node.js applications are divided into modules.

A module is simply a file containing reusable code.

math.js

function add(a, b) {
	return a + b;
}

module.exports = add;

app.js

const add = require("./math");
console.log(add(5, 4));

Output:

8

Benefits:

  • Better organization
  • Reusable code
  • Easier maintenance

CommonJS Module System

Node.js traditionally use CommonJS.

Export:

module.exports = myFunction;

Import:

const myFunction = require("./myFile");

Modern Node.js also supports ES Modules:

export function add() {}

import { add } from "./math.js";

Events in Node.js

Node.js follows an event-driven architecture.

Using EventEmitter:

 

Buy Me A Coffee

Leave a comment

Your email address will not be published. Required fields are marked *