Skip to main content

Command Palette

Search for a command to run...

Setting Up Your First Node.js Application Step-by-Step

Updated
•5 min read•View as Markdown

Installing Node.js

Before writing backend applications using :contentReference[oaicite:0]{index=0}, Node.js must first be installed on the system. Node.js allows JavaScript to run outside the browser. Without Node.js:

  • JavaScript mainly runs inside browsers

  • backend development becomes impossible

Why Installing Node.js Matters

Installing Node.js provides:

  • Node runtime

  • npm (Node Package Manager)

  • backend execution environment

This allows developers to:

  • run JavaScript from terminal

  • create servers

  • install packages

  • build backend applications

Downloading Node.js

Node.js can be downloaded from the official website: Node.js Official Website Usually two versions appear:

LTS Version

Long Term Support

Recommended for most developers. More stable.

Current Version

Contains latest features but may change more frequently. Beginners usually prefer:

LTS version.

Installing Node.js on macOS

Since you use macOS, installation is commonly done using:

  • installer package

  • Homebrew

Using Homebrew

Example:

brew install node

This installs:

  • node

  • npm

Checking Installation Using Terminal

After installation, developers verify setup using terminal commands.

Checking Node.js Version

Command:

node -v

Example output:

v22.1.0

This confirms:

  • Node.js installed successfully

Checking npm Version

Command:

npm -v

Example output:

10.5.0

This confirms npm installation.

Why Version Checking Matters

Sometimes:

  • installation fails

  • PATH variables break

  • wrong versions exist

Checking versions helps verify environment setup correctly.

Understanding Node REPL

One of the most interesting beginner features in Node.js is:

REPL

REPL stands for:

Read
Eval
Print
Loop

What REPL Actually Does

REPL allows developers to:

  • write JavaScript directly in terminal

  • execute code instantly

  • test small snippets quickly

Without creating separate files.

Starting REPL

Command:

node

After running:

  • interactive Node environment opens

Example:

>

Now JavaScript can be executed directly.

Example Inside REPL

Example:

2 + 3

Output:

5

Another Example

console.log("Hello")

Output:

Hello

Why REPL Is Useful

REPL is very useful for:

  • testing JavaScript quickly

  • experimenting with code

  • debugging logic

  • learning Node basics

Understanding REPL Name

Read

Reads input.

Eval

Executes code.

Print

Displays output.

Loop

Repeats continuously.

Exiting REPL

To exit:

.exit

or press:

Ctrl + C

twice.

Creating First JS File

Real applications usually use JavaScript files instead of REPL.

Step 1 — Create Project Folder

Example:

mkdir myapp

Step 2 — Open Folder

Example:

cd myapp

Step 3 — Create File

Example:

touch app.js

Now:

app.js

becomes your first Node.js file.

Writing First JavaScript Code

Inside:

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

Very simple.

But extremely important first step.

Running Script Using Node Command

After writing code, Node.js executes file using:

node filename

Example:

node app.js

Output:

Hello Node.js

Understanding What Happened

Node.js:

  1. reads file

  2. executes JavaScript

  3. prints output

This is how backend scripts run.

Why Running Files Matters

Real backend applications contain:

  • APIs

  • servers

  • databases

  • authentication logic

All of them eventually execute through Node runtime.

Writing Hello World Server

One of the most famous beginner examples in Node.js is creating first HTTP server.

Why Server Matters

Server is basically:

a program that listens for requests and sends responses.

Websites and APIs work using servers.

Basic Hello World Server

Example:

const http = require("http");

const server = http.createServer((req, res) => {
  res.end("Hello World");
});

server.listen(3000, () => {
  console.log("Server running on port 3000");
});

Understanding This Code Step-by-Step

require("http")

Imports built-in HTTP module.

createServer()

Creates web server.

(req, res)

Handles:

  • incoming request

  • outgoing response

res.end()

Sends response back to browser.

listen(3000)

Starts server on port 3000.

Running the Server

Command:

node app.js

Terminal output:

Server running on port 3000

Accessing the Server

Open browser:

http://localhost:3000

Output:

Hello World

Understanding localhost

localhost means:

your own computer.

So browser communicates with server running on same machine.

What Is a Port?

Port is like a communication door.

Example:

localhost:3000

Means:

  • computer → localhost

  • application → port 3000

Different applications use different ports.

Real Server Flow

Browser
   ↓
Request
   ↓
Node.js Server
   ↓
Response
   ↓
Browser

This is the foundation of backend development.

Common Beginner Mistake

Many beginners forget saving file before running:

node app.js

Then updated changes do not appear.

Another Common Mistake

Beginners often close terminal accidentally. If terminal closes:

  • server also stops

Because Node process terminates.

Why First Server Is Important

This simple server teaches core backend concepts:

  • request

  • response

  • ports

  • HTTP communication

  • server listening

Almost every backend framework builds on these same ideas.

Understanding the Bigger Picture

Even advanced frameworks like:

  • :contentReference[oaicite:2]{index=2}

  • :contentReference[oaicite:3]{index=3}

ultimately create HTTP servers internally.

So understanding raw Node server basics is extremely important.

Final Understanding

Installing :contentReference[oaicite:4]{index=4} allows JavaScript to run outside browsers and enables backend development.

Developers can:

  • verify installation using terminal

  • experiment using Node REPL

  • create JavaScript files

  • execute scripts using node command

And using Node’s built-in HTTP module, developers can create simple servers that listen for requests and send responses.

This becomes the foundation of modern backend development using JavaScript.

1 views