Setting Up Your First Node.js Application Step-by-Step
Imagine you have learned JavaScript and can build logic in the browser. Now you want to create servers, APIs, and backend applications. The first question most beginners ask is:
"How do I start my first Node.js application?"
In this article, we'll learn how to install Node.js, verify the installation, understand the Node REPL, create our first JavaScript file, run it using Node, and finally build a simple Hello World server.
The Problem
JavaScript originally worked only inside browsers.
If you wanted to run JavaScript outside the browser, create servers, read files, or interact with databases, you couldn't do it.
For example:
console.log("Hello World");
This code runs in Chrome, Firefox, or Edge.
But how can we run it directly from our computer's terminal?
This is where Node.js comes in.
What is Node.js?
Node.js is a JavaScript runtime that allows JavaScript code to run outside the browser.
Think of it like this:
Browser = Runs JavaScript inside websites
Node.js = Runs JavaScript on your computer
With Node.js, you can build:
Backend APIs
Web servers
Real-time applications
Command-line tools
Full-stack applications
Installing Node.js
Visit the official Node.js website:
Download the LTS (Long-Term Support) version.
The installation process is similar across operating systems:
Download Node.js
Run the installer
Follow the setup wizard
Complete installation
After installation, Node.js and npm are installed automatically.
What is npm?
npm stands for Node Package Manager.
It helps us install and manage packages used in Node.js projects.
Checking the Installation
Open your terminal and run:
node -v
Example output:
v24.1.0
Now check npm:
npm -v
Example output:
11.2.0
If both commands show versions, Node.js is installed successfully.
Understanding Node REPL
Before creating files, let's understand REPL.
REPL stands for:
Read
Evaluate
Print
Loop
Node REPL is an interactive environment where you can execute JavaScript code directly from the terminal.
Start REPL:
node
You will see something like:
>
Now type:
2 + 3
Output:
5
Try:
const name = "Ashish";
name
Output:
'Ashish'
Node immediately reads your code, executes it, prints the result, and waits for the next command.
That's why it's called REPL.
Exit REPL:
.exit
or
Ctrl + C
(two times)
Creating Your First JavaScript File
Create a file named:
app.js
Add the following code:
console.log("Hello from Node.js");
Save the file.
Running the Script Using Node
Open terminal in the same folder.
Run:
node app.js
Output:
Hello from Node.js
Congratulations!
You just executed your first Node.js application.
How Node Executes a Script
The execution flow looks like this:
app.js
↓
Node Runtime
↓
JavaScript Engine
↓
Output in Terminal
Step-by-Step
Node reads the file.
Node sends JavaScript code to the JavaScript engine.
The engine executes the code.
Result appears in the terminal.
Building a Hello World Server
Printing text is useful, but backend developers create servers.
Let's create our first web server.
Create a file:
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
const http = require("http");
Node provides a built-in HTTP module for creating servers.
Create Server
const server = http.createServer((req, res) => {
res.end("Hello World");
});
Whenever a request arrives:
reqcontains request information.resis used to send a response.
Start Listening
server.listen(3000);
The server starts listening on port 3000.
Running the Server
Execute:
node app.js
Output:
Server running on port 3000
Open your browser and visit:
http://localhost:3000
You will see:
Hello World
Your first Node.js server is now running successfully.
Node Execution Flow
The overall flow looks like this:
Browser Request
↓
Node.js Server
↓
Request Handler
↓
Response Sent
↓
Browser Displays Result