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

If you want to build backend applications using JavaScript, the first thing you need is **Node.js**. It allows JavaScript to run outside the browser, making it possible to create servers, APIs, command-line tools, and much more.

In this beginner-friendly guide, you’ll learn how to:

*   Install Node.js
    
*   Verify the installation
    
*   Understand the Node REPL
    
*   Create and run your first JavaScript file
    
*   Build a simple Hello World server
    

By the end, you’ll have your very first Node.js application running successfully.

* * *

# What is Node.js?

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. In simple words, it allows you to run JavaScript directly on your computer instead of only inside a browser.

Before Node.js existed:

*   JavaScript mostly worked only inside browsers
    

After Node.js:

*   JavaScript could power servers and backend applications too
    

This made JavaScript a full-stack programming language.

* * *

# Step 1: Installing Node.js

To start using Node.js, you first need to install it on your system.

## Download Node.js

Visit the official website:

[Node.js Official Website](https://nodejs.org?utm_source=chatgpt.com)

You’ll usually see two versions:

*   **LTS (Recommended)** → Stable and best for beginners
    
*   **Current** → Latest features
    

Choose the **LTS version**.

* * *

## Installation Process

The setup is mostly the same across operating systems:

1.  Download the installer
    
2.  Open the installer
    
3.  Click “Next” through the setup steps
    
4.  Finish installation
    

Node.js automatically installs:

*   Node runtime
    
*   npm (Node Package Manager)
    

npm is used later to install libraries and tools.

* * *

# Step 2: Checking the Installation

After installation, open your terminal or command prompt.

Run this command:

```bash
node -v
```

You should see an output similar to:

```bash
v22.1.0
```

This means Node.js is installed successfully.

Now check npm:

```bash
npm -v
```

Example output:

```bash
10.7.0
```

If both commands work, your setup is complete.

* * *

# Understanding the Node REPL

Before writing actual files, it’s useful to understand something called the **REPL**.

## What is REPL?

REPL stands for:

*   **R**ead
    
*   **E**valuate
    
*   **P**rint
    
*   **L**oop
    

It is an interactive environment where Node.js reads your code, runs it instantly, shows the result, and waits for the next command.

Think of it like a calculator for JavaScript.

* * *

## Starting the REPL

In your terminal, type:

```bash
node
```

You’ll see something like:

```bash
>
```

Now you can write JavaScript directly.

Example:

```javascript
2 + 3
```

Output:

```javascript
5
```

Another example:

```javascript
const name = "Hitesh"
```

Then:

```javascript
name
```

Output:

```javascript
'Hitesh'
```

* * *

## Exiting the REPL

To exit:

```bash
.exit
```

Or press:

```bash
Ctrl + C
```

twice.

* * *

# Step 3: Creating Your First JavaScript File

Now let’s create an actual Node.js program.

## Create a Project Folder

Create a new folder anywhere on your computer.

Example:

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

Open this folder in your code editor.

* * *

## Create a JavaScript File

Inside the folder, create a file named:

```text
app.js
```

Add this code:

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

* * *

# Step 4: Running the Script Using Node

Open terminal inside your project folder.

Run:

```bash
node app.js
```

Output:

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

Congratulations 🎉

You just executed your first Node.js application.

* * *

# How Node Executes Your Script

Here’s the basic flow:

```text
app.js → Node.js Runtime → Output in Terminal
```

## Explanation

1.  You write code in `app.js`
    
2.  Node.js reads the file
    
3.  The V8 engine executes the JavaScript
    
4.  Output appears in the terminal
    

* * *

# Node Execution Flow Diagram

```text
┌─────────────┐
│ JavaScript  │
│   File      │
│  (app.js)   │
└──────┬──────┘
       │
       ▼
┌─────────────┐
│   Node.js   │
│   Runtime   │
└──────┬──────┘
       │
       ▼
┌─────────────┐
│   Console   │
│   Output    │
└─────────────┘
```

* * *

# Step 5: Writing Your First Hello World Server

Now let’s create a very basic web server using Node.js.

Update your `app.js` file:

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

const server = http.createServer((request, response) => {
  response.write("Hello World");
  response.end();
});

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

* * *

# Understanding the Code

## Importing the HTTP Module

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

Node.js provides built-in modules. The `http` module helps create web servers.

* * *

## Creating the Server

```javascript
http.createServer()
```

This creates a server that listens for requests from browsers.

* * *

## Sending a Response

```javascript
response.write("Hello World");
```

This sends text back to the browser.

* * *

## Starting the Server

```javascript
server.listen(3000)
```

This starts the server on port 3000.

* * *

# Running the Server

Run the file again:

```bash
node app.js
```

You’ll see:

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

Now open your browser and visit:

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

You should see:

```text
Hello World
```

Your first Node.js server is now running successfully 🚀

* * *

# Server Request Flow Diagram

```text
Browser Request
       │
       ▼
Node.js Server
       │
       ▼
Response Sent Back
       │
       ▼
Browser Displays Output
```

* * *

# Common Beginner Mistakes

## 1\. Wrong Folder in Terminal

If Node cannot find your file:

```bash
Error: Cannot find module
```

Make sure terminal is opened in the correct folder.

* * *

## 2\. Typing File Name Incorrectly

This will fail:

```bash
node App.js
```

if your actual file name is:

```text
app.js
```

File names are case-sensitive on many systems.

* * *

## 3\. Port Already in Use

If port 3000 is busy, change:

```javascript
server.listen(3000)
```

to another port like:

```javascript
server.listen(5000)
```

* * *

# Why Learning Node.js Matters

Node.js is widely used for:

*   Backend APIs
    
*   Real-time chat applications
    
*   Streaming platforms
    
*   Command-line tools
    
*   Full-stack JavaScript applications
    

Popular companies using Node.js include:

*   Netflix
    
*   PayPal
    
*   LinkedIn
    

* * *

# Final Thoughts

You’ve now learned the complete basics of setting up your first Node.js application:

✅ Installed Node.js ✅ Verified installation ✅ Used the Node REPL ✅ Created your first JavaScript file ✅ Executed a script using Node ✅ Built a Hello World server

This is the foundation of backend development with JavaScript.

Your next step could be learning:

*   File handling
    
*   Modules
    
*   npm packages
    
*   Express.js
    
*   APIs and databases
    

But before moving ahead, make sure you are comfortable running Node scripts and understanding how the server works internally.
