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


Welcome to your first dive into Node.js! In this guide we'll go from zero to a running “Hello World” web server—without any frameworks—so you can understand exactly what happens under the hood. The instructions are **OS‑neutral**; they work on Windows, macOS and Linux.

## Topics We’ll Cover

1.  Installing Node.js (the OS‑neutral way)
    
2.  Checking the installation in the terminal
    
3.  Understanding the Node REPL
    
4.  Creating and running your first JavaScript file
    
5.  Writing a Hello World HTTP server (no Express, just vanilla Node)
    
6.  Diagram ideas to visualise the flow
    

## Installing Node.js

Go to the official website: [nodejs.org](https://nodejs.org).  
You’ll see two big buttons – **LTS** (Long‑Term Support) and **Current**. For beginners, always pick the **LTS** version. It’s the most stable and widely used.

Download the installer for your operating system:

*   **Windows**: Run the `.msi` installer and follow the wizard. Make sure the box “Add to PATH” is ticked (it usually is).
    
*   **macOS**: Use the `.pkg` installer, or if you prefer a package manager, `brew install node` works perfectly.
    
*   **Linux**: Use your distribution’s package manager (e.g., `sudo apt install nodejs` on Ubuntu/Debian) or the NodeSource binary distributions for the latest LTS.
    

After the installation finishes, you’re ready to verify it.

## Checking the Installation Using the Terminal

Open your terminal (Command Prompt, PowerShell, Terminal, etc.) and type:

```bash
node -v
```

You should see a version number, for example: `v20.11.0`.  
Node ships with **npm** (Node Package Manager) by default. Check it the same way:

```bash
npm -v
```

If both commands print a version, you’re all set! 🎉

## Understanding the Node REPL

REPL stands for **Read‑Eval‑Print Loop**. It’s an interactive programming environment that:

*   **Reads** your input
    
*   **Evaluates** it as JavaScript
    
*   **Prints** the result
    
*   **Loops** back and waits for more input
    

It’s perfect for quick experiments, testing small snippets, or just playing around with JavaScript without creating a file.

### Launch the REPL

In your terminal, just type:

```bash
node
```

You’ll see a `>` prompt. Try these lines one by one:

```js
> 2 + 3
5
> const greeting = 'Hello, Node!'
undefined
> greeting
'Hello, Node!'
> console.log('REPL is fun!')
REPL is fun!
undefined
> .exit   // (or press Ctrl+D / Ctrl+C twice to leave)
```

The REPL is your instant Node.js playground – you’ll use it a lot while learning.

## Creating Your First JavaScript File

Now let’s write a script that runs in the *runtime* (not the REPL).

1.  Create a new folder for your project, e.g. `my-first-node-app`.
    
2.  Inside it, create a file called `app.js`.
    
3.  Open `app.js` in your favourite code editor and add:
    

```js
// app.js
console.log('My first Node.js script is alive!');
```

Save the file.

### Run the Script

In the terminal, make sure you are inside the project folder, then execute:

```bash
node app.js
```

You should immediately see:

```plaintext
My first Node.js script is alive!
```

That’s the whole **Script → Runtime → Output** flow in action:

*   **Script** → your `app.js` file
    
*   **Runtime** → the Node.js process that reads, compiles, and executes your JavaScript (using Google’s V8 engine and libuv for I/O)
    
*   **Output** → whatever your script logs (or any side effects)
    

> **Diagram idea** (you can sketch this in your notes or use a tool like Excalidraw):  
> *Write code in file* → *Run* `node app.js` → *V8 engine executes* → *Output appears in terminal*

## Writing a “Hello World” HTTP Server (No Frameworks)

Let’s take it one step further and create a real web server using only Node’s built-in `http` module.

Edit (or create) a file called `server.js` with the following content:

```js
// server.js
const http = require('node:http');   // Use 'http' if 'node:http' isn't supported

const hostname = '127.0.0.1';        // localhost
const port = 3000;

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

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
```

Save the file and run it:

```bash
node server.js
```

Your terminal will print:  
`Server running at http://127.0.0.1:3000/`

Now open a browser and go to `http://localhost:3000`. You’ll see the classic **Hello, World!** – straight from your own server, zero third‑party code required.

### How it works (the execution flow)

1.  `require('node:http')` loads the built‑in HTTP module.
    
2.  `http.createServer()` returns a server instance that listens for requests.
    
3.  Each incoming request triggers the callback `(req, res)` → we set a 200 status, a plain text header, and end the response with `"Hello, World!"`.
    
4.  `server.listen()` starts the server and logs a message once it’s ready.
    

> **Diagram idea – Node execution flow for a web server:**  
> *Incoming request* → *HTTP module parses it* → *Your callback runs (on the event loop)* → *Response sent back*  
> You can visualise this as:  
> Client → **Node.js Runtime (V8 + libuv)** → Your JavaScript → Response → Client

## Additional Diagram Ideas

If you’re writing this as a blog post, you can create simple illustrations to explain the concepts:

*   **Script → runtime → output flow**  
    `[your code]` → (arrow) `[node command]` → (arrow) `[V8 execution]` → (arrow) `[terminal / browser output]`
    
*   **REPL cycle**  
    `Read user input` → `Evaluate as JavaScript` → `Print result` → `Loop back`
    
*   **Node.js architecture (simplified)**  
    `JavaScript Code` → `V8 Engine` → `Node.js APIs (fs, http, etc.)` → `libuv (async I/O)` → `Operating System`
    

You can draw these with hand‑drawn boxes, use Mermaid (if your blog platform supports it), or even simple ASCII art. The goal is to help readers understand that Node.js is more than just a command – it’s an entire runtime built on V8 and libuv.

* * *

## Wrapping Up

You’ve just:

*   Installed Node.js (OS‑neutrally)
    
*   Verified the installation
    
*   Played with the REPL
    
*   Run your first script
    
*   Built a real HTTP server with zero frameworks
    

From here, you can start adding routes, reading files from disk, or exploring npm packages – but the foundation you’ve built today is solid and will serve you throughout your Node.js journey.
