Skip to main content

Command Palette

Search for a command to run...

Getting Started with Node.js: Your First Steps into Server-Side JavaScript

Published
6 min read

If you've ever wanted to run JavaScript outside of a browser — on a server, in a terminal, or as a backend — Node.js is your entry point. This guide walks you through everything from installation to writing your first HTTP server, with zero frameworks involved.


What Is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 engine. It lets you execute JavaScript code directly on your machine — not inside a browser tab. This opens the door to building web servers, command-line tools, APIs, and much more using the same language you already know from the frontend.


1. Installing Node.js

The simplest and most reliable way to install Node.js on any operating system is through the official website:

👉 https://nodejs.org

Node.js Download Page

You'll see two versions available:

Version Description
LTS (Long-Term Support) Recommended for most users — stable and production-ready
Current Latest features, but less stable

Pick LTS if you're just getting started.

The installer handles everything for you — Node.js and npm (Node Package Manager) will both be installed automatically.

Installation by OS

  • Windows / macOS: Download the .msi or .pkg installer from the website and follow the prompts.

  • Linux: Use your package manager or the official install script via NodeSource.

# Example for Debian/Ubuntu Linux
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

2. Checking Your Installation

Once the installation finishes, open your terminal (Command Prompt, PowerShell, or any Unix shell) and run the following commands:

node --version

You should see something like:

v20.12.0

Then check npm:

npm --version

Expected output:

10.5.0

If both commands print version numbers, you're all set. If you get a "command not found" error, try restarting your terminal or re-running the installer.


3. Understanding the Node REPL

Before you write any files, let's explore the REPL — one of Node's most useful tools for learning and quick experimentation.

REPL stands for Read–Eval–Print Loop. It's an interactive shell that:

  1. Reads the code you type

  2. Evaluates (runs) it immediately

  3. Prints the result to the screen

  4. Loops back, waiting for your next input

Think of it like the browser's DevTools Console — but for your machine.

Launching the REPL

Just type node in your terminal with no arguments:

node

You'll see a > prompt appear:

Welcome to Node.js v20.12.0.
Type ".help" for more information.
>

Playing Around

> 2 + 2
4

> "Hello, " + "Node!"
'Hello, Node!'

> const name = "World"
undefined

> `Hello, ${name}!`
'Hello, World!'

> Math.pow(2, 10)
1024

You can even write multi-line code — Node detects incomplete expressions and waits for you to finish.

Exiting the REPL

Press Ctrl + C twice, or type .exit and hit Enter.


4. Creating Your First JavaScript File

The REPL is great for quick tests, but real programs live in .js files.

Create a new file called hello.js. You can use any text editor — VS Code, Notepad, nano, whatever you prefer.

// hello.js

const message = "Hello from Node.js!";
console.log(message);

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
console.log("Doubled:", doubled);

Save it to a folder you can navigate to in your terminal.


5. Running a Script with the node Command

Now let's run it. In your terminal, navigate to the folder where hello.js is saved:

cd path/to/your/folder

Then run:

node hello.js

You should see:

Hello from Node.js!
Doubled: [ 2, 4, 6, 8, 10 ]

That's it — your JavaScript is now executing on the machine itself, not inside a browser.

How It Works: Script → Runtime → Output

Here's a simple mental model of what's happening when you run node hello.js:

┌─────────────┐     ┌──────────────────┐     ┌──────────────┐
│  hello.js   │────▶│  Node.js Runtime │────▶│   Output     │
│  (your code)│     │  (V8 + Node APIs)│     │  (Terminal)  │
└─────────────┘     └──────────────────┘     └──────────────┘

Node reads your file, passes it through the V8 JavaScript engine (the same one Chrome uses), and executes it — printing results directly to your terminal.


6. Writing a Hello World HTTP Server

Now for the exciting part. Let's build a basic web server using only Node's built-in http module — no Express, no frameworks.

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;                        // HTTP 200 OK
  res.setHeader('Content-Type', 'text/plain'); // Plain text response
  res.end('Hello, World!\n');                  // Send response body
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://\({hostname}:\){port}/`);
});

Running the Server

node server.js

You'll see:

Server running at http://127.0.0.1:3000/

Now open your browser and go to http://localhost:3000. You'll see:

Hello, World!

Understanding the Code

Let's break it down line by line:

Line What it does
require('http') Imports Node's built-in HTTP module
http.createServer(...) Creates a server that calls your function on every request
req The incoming request object (URL, headers, method, etc.)
res The response object — you write to this to send data back
res.statusCode = 200 Sets the HTTP status (200 = OK)
res.end(...) Sends the response body and ends the connection
server.listen(port, ...) Starts the server and listens on the given port

To stop the server, press Ctrl + C in your terminal.


Node Execution Flow (Visual Overview)

Here's the full picture of how Node.js processes an incoming HTTP request:

Browser Request
      │
      ▼
┌─────────────────────┐
│  server.listen()    │  ◀── Waiting for connections
└─────────┬───────────┘
          │ New request arrives
          ▼
┌─────────────────────┐
│  Callback Function  │  ◀── (req, res) => { ... }
│  (req, res)         │
└─────────┬───────────┘
          │
          ▼
┌─────────────────────┐
│  res.end(data)      │  ◀── Sends response back
└─────────┬───────────┘
          │
          ▼
     Browser renders
     "Hello, World!"

What's Next?

You've now:

  • Installed Node.js

  • Verified it works from the terminal

  • Explored the interactive REPL

  • Written and run your first .js file

  • Built a working HTTP server from scratch