Skip to main content

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

Updated 6 min readMarkdown

Most beginners install Node.js, type random commands from YouTube, see something working somehow, and then move on without understanding what actually happened.

Then one day terminal throws an error and their entire confidence evaporates.

So let’s do this properly.

This article will help you:

  • Install Node.js

  • Verify installation

  • Understand Node REPL

  • Create your first JavaScript file

  • Run scripts using Node

  • Build your first Hello World server

No frameworks. No unnecessary complexity. Just core Node.js fundamentals.


What is Node.js?

Node.js is a JavaScript runtime built on Chrome’s V8 engine.

Normally, JavaScript runs inside browsers.

Node.js allows JavaScript to run outside the browser directly on your computer.

That means JavaScript can now:

  • Create servers

  • Read files

  • Handle databases

  • Build APIs

  • Automate tasks

  • Run backend applications

Without Node.js, JavaScript was basically trapped inside browser tabs.


Installing Node.js

Visit the official website:

https://nodejs.org

Download the LTS (Long Term Support) version.

LTS is stable and recommended for most developers.

Avoid experimental versions unless you specifically need them.


Installation Steps (OS Neutral)

The installation process is mostly similar across:

  • Windows

  • macOS

  • Linux

General steps:

  1. Download installer

  2. Run setup

  3. Keep default settings

  4. Finish installation

Node.js installation also installs:

  • node

  • npm (Node Package Manager)

npm helps install external libraries and packages.


Checking Node.js Installation

After installation, open terminal or command prompt.

Run:

node -v

Example output:

v22.3.0

Now check npm:

npm -v

Example:

10.8.1

If versions appear successfully, Node.js is installed correctly.

If terminal says:

command not found

restart the terminal or reinstall Node.js properly.


Understanding Node REPL

Before writing files, let’s understand the REPL.

REPL stands for:

Read → Evaluate → Print → Loop

This is an interactive environment where Node.js executes JavaScript line by line.

Start REPL:

node

You’ll see:

>

Now type:

2 + 3

Output:

5

Try more:

const name = "Gagan";
console.log(name);

The REPL is useful for:

  • testing small code snippets

  • debugging

  • experimenting quickly

Think of it like a JavaScript playground inside terminal.


Exiting the REPL

To exit:

.exit

or press:

Ctrl + C

twice.


Creating Your First JavaScript File

Create a folder:

first-node-app

Inside it, create:

app.js

Add this code:

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

Simple. Small. Important.


Running Your First Node.js Script

Open terminal inside project folder.

Run:

node app.js

Output:

Hello from Node.js

Congratulations. You just executed JavaScript outside the browser.


How Node Executes Your File

When you run:

node app.js

Node.js does this internally:

Read File
    ↓
Compile JavaScript using V8 Engine
    ↓
Execute Code
    ↓
Display Output

Script → Runtime → Output Flow

app.js
   ↓
Node Runtime
   ↓
V8 Engine
   ↓
Execution
   ↓
Terminal Output

This flow is the foundation of every Node.js application.


Writing Your First Hello World Server

Now let’s create an actual server.

Update app.js:

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 the Code

Import HTTP Module

const http = require("http");

Node.js includes built-in modules.

http helps create web servers.


Create Server

http.createServer()

This creates a server object.

The callback runs whenever a client sends a request.


Request and Response

(req, res)
  • req → incoming request

  • res → outgoing response


Send Response

res.end("Hello World");

This sends response back to browser.


Start Server

server.listen(3000)

Server starts running on port 3000.


Running the Server

Execute:

node app.js

Output:

Server running on port 3000

Now open browser:

http://localhost:3000

You’ll see:

Hello World

And just like that, your laptop became a web server.


What is localhost?

localhost means:

Your own computer

Port 3000 is like a communication channel where your server listens for requests.

Different applications use different ports.

Examples:

Port Common Usage
3000 Development servers
5000 APIs
8080 Alternative web servers
80 Default HTTP

Common Beginner Mistakes

1. Running Wrong File Name

node index.js

when actual file is:

app.js

Terminal will immediately throw an error.


2. Forgetting to Save File

You update code. Run program. Nothing changes.

Because editor file was never saved.


3. Port Already in Use

Error:

EADDRINUSE

Means another application already uses that port.

Use another port:

server.listen(5000)

Why Node.js Became Popular

Node.js became huge because it allows:

  • JavaScript everywhere

  • fast execution using V8

  • event-driven architecture

  • scalable backend systems

Frontend and backend using the same language was a major industry shift.

Before Node.js:

Frontend → JavaScript
Backend → Different language

After Node.js:

Everything → JavaScript

Final Thoughts

Your first Node.js application is not about building something complex.

It’s about understanding:

  • how Node runs JavaScript

  • how terminal execution works

  • how servers respond to requests

  • how backend applications start

Every advanced Node.js project still relies on these same basics.

The difference between beginner and expert is usually not tools.

It’s understanding the fundamentals deeply instead of blindly copying tutorials.


Specification Section

Core Concepts Covered

Concept Meaning
Node.js JavaScript runtime outside browser
REPL Interactive JavaScript shell
node app.js Executes JS file
HTTP Module Built-in module for creating servers
localhost Your own machine
Port Communication channel

Important Commands

Check Node Version

node -v

Check npm Version

npm -v

Start REPL

node

Run JavaScript File

node app.js

Mental Model You Should Keep

Whenever you run:

node app.js

Think:

JavaScript File
    ↓
Node Runtime
    ↓
V8 Engine
    ↓
Execution
    ↓
Output

That’s the core execution pipeline of Node.js.

Master this first. Frameworks can wait.

1 view

No comments yet. Be the first to comment.