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

### 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](https://nodejs.org?utm_source=chatgpt.com) Usually two versions appear:

### LTS Version

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

```bash
brew install node
```

This installs:

*   node
    
*   npm
    

### Checking Installation Using Terminal

After installation, developers verify setup using terminal commands.

### Checking Node.js Version

Command:

```bash
node -v
```

Example output:

```text
v22.1.0
```

This confirms:

*   Node.js installed successfully
    

### Checking npm Version

Command:

```bash
npm -v
```

Example output:

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

```text
REPL
```

REPL stands for:

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

```bash
node
```

After running:

*   interactive Node environment opens
    

Example:

```bash
>
```

Now JavaScript can be executed directly.

### Example Inside REPL

Example:

```js
2 + 3
```

Output:

```text
5
```

### Another Example

```js
console.log("Hello")
```

Output:

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

```bash
.exit
```

or press:

```text
Ctrl + C
```

twice.

### Creating First JS File

Real applications usually use JavaScript files instead of REPL.

### Step 1 — Create Project Folder

Example:

```bash
mkdir myapp
```

### Step 2 — Open Folder

Example:

```bash
cd myapp
```

### Step 3 — Create File

Example:

```bash
touch app.js
```

Now:

```text
app.js
```

becomes your first Node.js file.

### Writing First JavaScript Code

Inside:

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

```bash
node filename
```

Example:

```bash
node app.js
```

Output:

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

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

```bash
node app.js
```

Terminal output:

```text
Server running on port 3000
```

### Accessing the Server

Open browser:

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

Output:

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

```text
localhost:3000
```

Means:

*   computer → localhost
    
*   application → port 3000
    

Different applications use different ports.

### Real Server Flow

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

This is the foundation of backend development.

### Common Beginner Mistake

Many beginners forget saving file before running:

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