Enough theory — let's get Node.js running and write something that actually executes.
Step 1: Install Node.js
Head to the official Node.js website and download the LTS (Long-Term Support) version — it's the stable, recommended build for most people, as opposed to the "Current" version which ships newer features but changes faster. Run the installer for your OS (Windows, macOS, or Linux) and accept the defaults; it will install both node (the runtime) and npm (its package manager) together.
Step 2: Confirm it installed correctly
Open a terminal and run:
node -v
npm -v
Each command should print a version number (something like v22.x.x and 10.x.x). If you see "command not found" instead, the installer likely didn't add Node to your system PATH — reinstalling usually fixes this.
Step 3: Set up a project folder
mkdir hello-node
cd hello-node
npm init -y
The npm init -y command generates a package.json file — a small manifest that describes your project (name, version, entry file, dependencies). You'll see this file in almost every Node.js project you ever touch.
Step 4: Write the app
Create a file named server.js inside that folder with the following code:
const http = require('http');
const server = http.createServer((request, response) => {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ status: 'ok', message: 'Node.js server is alive' }));
});
server.listen(4000, () => {
console.log('Listening on http://localhost:4000');
});
What this does, line by line:
require('http')pulls in Node's built-in HTTP module — no installation needed, it ships with Node itself.http.createServer(...)builds a server object. The function you pass in runs every single time a request comes in, and it receives two objects:request(details about what the client sent) andresponse(the tool you use to send something back).response.writeHead(200, {...})sets the HTTP status code (200 = success) and tells the client to expect JSON.response.end(...)sends the actual response body and closes it out.server.listen(4000, ...)starts the server listening for incoming connections on port 4000, and the callback runs once, right when the server successfully starts.
Step 5: Run it
node server.js
You should see Listening on http://localhost:4000 printed in your terminal, and the process will keep running (that's expected — a server has to stay alive to keep listening). Open http://localhost:4000 in a browser, or run curl http://localhost:4000 in another terminal tab, and you'll get back:
{"status":"ok","message":"Node.js server is alive"}
To stop the server, go back to the terminal where it's running and press Ctrl + C.
That's a real, working Node.js server — nine lines, zero external dependencies. Everything else you'll learn about Node.js builds on exactly this pattern: import a module, set up handlers, let the event loop do the waiting.