Setting Up Your First Node.js Application: A Step-by-Step Guide
Node.js lets you run JavaScript outside of a browser — on your machine, a server, or anywhere you need server-side logic. Before diving into frameworks like Express or Next.js, it's worth understanding what happens when you type node and press Enter. This guide walks you through everything from installation to your first running server, one step at a time.
1. Installing Node.js
The recommended way to install Node.js on any operating system is through the official website. You'll see two versions offered:
LTS (Long Term Support) — stable, production-ready. Use this for learning and most projects.
Current — latest features, updated frequently. Better suited for experimentation.
Download and run the installer for your OS (Windows .msi, macOS .pkg, or Linux via your package manager). The installer also bundles npm (Node Package Manager), which you'll use later for installing third-party libraries.
Tip for power users: If you switch between Node versions often, consider using a version manager like nvm (macOS/Linux) or nvm-windows.
2. Checking the Installation
Open your terminal (Command Prompt, PowerShell, or any Unix shell) and run:
node --version
You should see something like:
v20.11.0
Then verify npm is also installed:
npm --version
If both commands print version numbers, you're ready to go. If either fails, restart your terminal or revisit the installer.
3. Understanding the Node REPL
REPL stands for Read–Evaluate–Print Loop. It's an interactive shell that reads a line of JavaScript, evaluates it, prints the result, and waits for the next input — in a continuous loop.
To enter the REPL, simply type node with no arguments:
node
Your prompt changes to >. You can now type JavaScript directly:
> 2 + 2
4
> 'Hello'.toUpperCase()
'HELLO'
> const name = 'World'
undefined
> `Hello, ${name}!`
'Hello, World!'
The REPL is perfect for quickly testing expressions, exploring built-in methods, or debugging a small piece of logic without creating a file.
To exit, press Ctrl + C twice, or type .exit.
4. Creating Your First JavaScript File
The REPL is interactive but not persistent — everything disappears when you exit. For real programs, you write code in .js files.
Create a new file called hello.js in any folder you like:
// hello.js
const greeting = 'Hello, World!';
console.log(greeting);
That's it. No special boilerplate required — just plain JavaScript.
5. Running a Script with the node Command
Navigate to the folder containing hello.js in your terminal, then run:
node hello.js
You'll see:
Hello, World!
Here's what just happened — refer to the Script → Runtime → Output diagram above for a visual summary:
Node.js read your
hello.jsfile.The V8 engine parsed and compiled the JavaScript.
console.logwrote the string to standard output (your terminal).The process exited.
You can pass any .js file to the node command the same way. The file name is just a path — node src/index.js works just as well.
6. Writing Your First HTTP Server
Now for the part that makes Node.js special for backend development: handling web requests without a framework.
Node's built-in http module lets you create a server in just a few lines. Create a new file called server.js:
// server.js
const http = require('http');
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}/`);
});
Run it:
node server.js
Your terminal will print:
Server running at http://127.0.0.1:3000/
Open that URL in your browser or run curl http://127.0.0.1:3000 in another terminal — you'll receive Hello, World! as the response.
What's happening here:
http.createServer()registers a callback function that runs every time a request arrives.reqholds the incoming request (URL, headers, method).resis the outgoing response — you set the status, headers, and body, then callres.end()to send it.server.listen()binds the server to a port and starts waiting for connections.
This server keeps running (the event loop stays alive) until you stop it with Ctrl + C. The Node execution flow diagram at the top shows how this works internally — libuv watches the port for incoming connections and fires your callback each time one arrives.
What's Next?
You now have a working Node.js environment, a feel for the REPL, and a real HTTP server running on your machine — no framework needed. From here, you can explore:
Reading URL paths from
req.urlto serve different responsesParsing JSON request bodies
Reading files with the built-in
fsmoduleEventually, Express.js for routing and middleware
The fundamentals you've covered here are exactly what every Node framework builds on top of.