Skip to main content

Command Palette

Search for a command to run...

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

Updated
6 min readView as Markdown

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

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:

node -v

You should see an output similar to:

v22.1.0

This means Node.js is installed successfully.

Now check npm:

npm -v

Example output:

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:

  • Read

  • Evaluate

  • Print

  • Loop

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:

node

You’ll see something like:

>

Now you can write JavaScript directly.

Example:

2 + 3

Output:

5

Another example:

const name = "Hitesh"

Then:

name

Output:

'Hitesh'

Exiting the REPL

To exit:

.exit

Or press:

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:

my-first-node-app

Open this folder in your code editor.


Create a JavaScript File

Inside the folder, create a file named:

app.js

Add this code:

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

Step 4: Running the Script Using Node

Open terminal inside your project folder.

Run:

node app.js

Output:

Hello from Node.js

Congratulations 🎉

You just executed your first Node.js application.


How Node Executes Your Script

Here’s the basic flow:

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

┌─────────────┐
│ 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:

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

const http = require("http");

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


Creating the Server

http.createServer()

This creates a server that listens for requests from browsers.


Sending a Response

response.write("Hello World");

This sends text back to the browser.


Starting the Server

server.listen(3000)

This starts the server on port 3000.


Running the Server

Run the file again:

node app.js

You’ll see:

Server is running on port 3000

Now open your browser and visit:

http://localhost:3000

You should see:

Hello World

Your first Node.js server is now running successfully 🚀


Server Request Flow Diagram

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:

Error: Cannot find module

Make sure terminal is opened in the correct folder.


2. Typing File Name Incorrectly

This will fail:

node App.js

if your actual file name is:

app.js

File names are case-sensitive on many systems.


3. Port Already in Use

If port 3000 is busy, change:

server.listen(3000)

to another port like:

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.