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

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:

```text
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:

```bash
node -v
```

Example output:

```bash
v22.3.0
```

Now check npm:

```bash
npm -v
```

Example:

```bash
10.8.1
```

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

If terminal says:

```bash
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:

```text
Read → Evaluate → Print → Loop
```

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

Start REPL:

```bash
node
```

You’ll see:

```bash
>
```

Now type:

```js
2 + 3
```

Output:

```js
5
```

Try more:

```js
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:

```bash
.exit
```

or press:

```text
Ctrl + C
```

twice.

* * *

# Creating Your First JavaScript File

Create a folder:

```text
first-node-app
```

Inside it, create:

```text
app.js
```

Add this code:

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

Simple. Small. Important.

* * *

# Running Your First Node.js Script

Open terminal inside project folder.

Run:

```bash
node app.js
```

Output:

```bash
Hello from Node.js
```

Congratulations. You just executed JavaScript outside the browser.

* * *

# How Node Executes Your File

When you run:

```bash
node app.js
```

Node.js does this internally:

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

* * *

# Script → Runtime → Output Flow

```text
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`:

```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

```js
const http = require("http");
```

Node.js includes built-in modules.

`http` helps create web servers.

* * *

## Create Server

```js
http.createServer()
```

This creates a server object.

The callback runs whenever a client sends a request.

* * *

## Request and Response

```js
(req, res)
```

*   `req` → incoming request
    
*   `res` → outgoing response
    

* * *

## Send Response

```js
res.end("Hello World");
```

This sends response back to browser.

* * *

## Start Server

```js
server.listen(3000)
```

Server starts running on port `3000`.

* * *

# Running the Server

Execute:

```bash
node app.js
```

Output:

```bash
Server running on port 3000
```

Now open browser:

```text
http://localhost:3000
```

You’ll see:

```text
Hello World
```

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

* * *

# What is `localhost`?

`localhost` means:

```text
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

```bash
node index.js
```

when actual file is:

```text
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:

```bash
EADDRINUSE
```

Means another application already uses that port.

Use another port:

```js
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:

```text
Frontend → JavaScript
Backend → Different language
```

After Node.js:

```text
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

```bash
node -v
```

### Check npm Version

```bash
npm -v
```

### Start REPL

```bash
node
```

### Run JavaScript File

```bash
node app.js
```

* * *

## Mental Model You Should Keep

Whenever you run:

```bash
node app.js
```

Think:

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

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

Master this first. Frameworks can wait.
