Setting Up Your First Node.js Application Step-by-Step
Ready to start with Node.js? Let's get everything installed and your first application running. This guide works on Windows, macOS, and Linux.
Installing Node.js
The easiest way is to download the installer from the official website:
- Go to nodejs.org
- Download the LTS (Long Term Support) version for your operating system
- Run the installer and follow the prompts
If you prefer a more flexible approach, use a version manager like nvm (Node Version Manager). It lets you switch between Node versions easily.
Checking Your Installation
Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and run:
node --version
npm --version
If everything installed correctly, you'll see version numbers like v20.10.0 and 10.0.0. That's it—you're ready to go.
Understanding the Node REPL
REPL stands for Read-Eval-Print Loop. It's an interactive way to run JavaScript directly in your terminal.
Type node and press enter:
$ node
>
The > prompt is waiting for JavaScript. Try some commands:
> 2 + 2
4
> const greeting = "Hello, World!";
undefined
> console.log(greeting);
Hello, World!
>
Type .exit or press Ctrl+C twice to leave the REPL.
Creating Your First JavaScript File
Create a new file called hello.js with any text editor. Add this code:
console.log("Hello from Node.js!");
Now run it from your terminal:
node hello.js
You should see Hello from Node.js! printed to the screen. That's your first Node script.
Writing a Simple HTTP Server
Now let's make something that actually responds like a web server:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from the Node.js server!');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Save this as server.js and run:
node server.js
Open your browser and go to http://localhost:3000. You'll see your message. Congratulations—you just built a web server in about 5 lines of code.
What Just Happened
Here's the flow:
server.js → Node.js runtime → HTTP server on port 3000
Browser → localhost:3000 → Response displayed
Node loaded your file, executed the JavaScript, and kept the server running, listening for requests.
Wrapping Up
You now have Node.js installed, know how to run JavaScript interactively, can execute script files, and have built a working HTTP server. That's the foundation everything else in Node.js builds on.
From here, you can explore npm (the package manager), install dependencies like Express, and start building real applications. The basics are in place—now it's up to you to explore.
