Getting Started with Node.js: Installation, REPL, and Your First Server
Node.js opens the door to running JavaScript outside the browser — on your laptop, on a server, or anywhere code can execute. This guide walks you through installing Node.js, verifying it works, exploring the interactive REPL environment, writing your first script file, and building a working HTTP server. No frameworks, no dependencies, just Node.js and a terminal.
Installing Node.js
What You Need
Node.js runs on Windows, macOS, and Linux. You only need:
- A computer with one of those operating systems
- Administrator access (to install software)
- An internet connection
- A terminal or command prompt
Step 1: Download the Installer
Visit the official Node.js website: https://nodejs.org
You will see two versions offered:
| Version | What It Means | Which to Choose |
|---|---|---|
| LTS (Long Term Support) | Stable, tested, receives bug fixes for 30+ months | Choose this for learning and production |
| Current | Latest features, updated frequently | Choose this only if you need cutting-edge features |
Download the LTS installer for your operating system. The website automatically detects whether you need Windows, macOS, or Linux.
Step 2: Run the Installer
On Windows:
- Double-click the downloaded
.msifile - Follow the installation wizard (accept defaults)
- The installer adds Node.js to your system PATH automatically
On macOS:
- Double-click the downloaded
.pkgfile - Follow the installation wizard
- Alternatively, if you use Homebrew:
brew install node
On Linux:
- The downloaded file is typically a tarball or package
- Or use your package manager:
- Ubuntu/Debian:
sudo apt install nodejs npm - Fedora:
sudo dnf install nodejs - Arch:
sudo pacman -S nodejs npm
- Ubuntu/Debian:
Step 3: Verify the Installation
Open your terminal (or command prompt on Windows) and run:
node --version
You should see output similar to:
v20.12.1
The exact number will vary depending on which LTS version is current, but any version 18 or higher is fine for everything in this guide.
Also verify that npm (Node Package Manager) installed alongside Node.js:
npm --version
Output:
10.5.0
If both commands print version numbers, Node.js is installed correctly.
Checking Installation Using Terminal
Opening Your Terminal
| Operating System | How to Open Terminal |
|---|---|
| Windows | Press Win + R, type cmd, press Enter. Or search for "Command Prompt" or "PowerShell" |
| macOS | Press Cmd + Space, type Terminal, press Enter |
| Linux | Press Ctrl + Alt + T on most distributions. Or search for "Terminal" |
Essential Check Commands
Run these commands one by one. Each confirms a different aspect of your installation:
# Check Node.js version
node --version
# Check npm version
npm --version
# Check where Node.js is installed (shows the file path)
which node # On macOS/Linux
where node # On Windows
# Check Node.js help (lists available commands)
node --help
Troubleshooting Common Issues
| Problem | Likely Cause | Solution |
|---|---|---|
node: command not found |
PATH not configured | Restart terminal, or reinstall with "Add to PATH" option |
| Version is very old (v10, v12) | System package manager installed outdated version | Download directly from nodejs.org instead |
npm missing |
Incomplete installation | Reinstall Node.js (npm comes bundled) |
| Permission errors on Linux | Missing sudo | Use sudo for global installs, or configure npm to use a user directory |
Understanding Node REPL
What REPL Means
REPL stands for Read-Eval-Print Loop. It is an interactive programming environment that:
- Reads the code you type
- Evaluates (executes) that code
- Prints the result back to you
- Loops back to step 1, waiting for more input
Think of REPL as a calculator that understands JavaScript. You type an expression, press Enter, and see the result immediately. No files to create, no save button to press — just instant feedback.
Why REPL Matters
- Experimentation: Test code snippets without creating files
- Learning: See what JavaScript expressions evaluate to in real time
- Debugging: Inspect variables and test functions interactively
- Exploration: Check what properties an object has by typing its name
Starting the Node REPL
In your terminal, simply type:
node
The prompt changes to a single > character. You are now inside the Node.js REPL.
Welcome to Node.js v20.12.1.
Type ".help" for more information.
>
Your First REPL Session
Type each line and press Enter after each one. Watch what happens:
> 2 + 2
4
> "Hello, " + "Node.js"
'Hello, Node.js'
> let name = "Developer"
undefined
> name
'Developer'
> console.log("Welcome, " + name)
Welcome, Developer
undefined
> typeof name
'string'
> [1, 2, 3].map(x => x * 2)
[ 2, 4, 6 ]
What you observed:
- Arithmetic works immediately
- String concatenation returns the combined string
- Variables can be declared with
letand inspected by name -
console.logprints to the terminal, then returnsundefined - Array methods work exactly as in browser JavaScript
REPL Special Commands
Commands that start with a dot are instructions to the REPL itself, not JavaScript code:
| Command | What It Does |
|---|---|
.help |
Shows all available REPL commands |
.exit |
Exits the REPL (or press Ctrl + C twice, or Ctrl + D) |
.clear |
Clears the local context (resets variables) |
.save filename.js |
Saves all commands entered in this session to a file |
.load filename.js |
Loads and executes a JavaScript file in the REPL |
Try it:
> .help
> let test = "saving this"
> .save mysession.js
Session saved to: mysession.js
> .exit
Exiting the REPL
You can exit in three ways:
- Type
.exitand press Enter - Press
Ctrl + Ctwice - Press
Ctrl + D(sends EOF signal)
Creating First JS File
Why Move from REPL to Files
The REPL is excellent for quick experiments, but real applications live in files. Files let you:
- Save code permanently
- Edit and refine logic
- Run the same code repeatedly
- Share code with others
- Build applications larger than a few lines
Step 1: Create a Project Directory
In your terminal, create a folder for your Node.js experiments:
# Create directory
mkdir my-node-project
# Enter directory
cd my-node-project
# Verify you are in the right place
pwd # macOS/Linux
cd # Windows (shows current path)
Step 2: Create Your First JavaScript File
Create a file named hello.js. You can use any text editor:
Using a code editor (VS Code, Sublime, etc.):
Open the my-node-project folder in your editor and create a new file called hello.js.
Using terminal commands:
# On macOS/Linux (creates file with content)
echo 'console.log("Hello from a file!");' > hello.js
# On Windows PowerShell
Set-Content hello.js 'console.log("Hello from a file!");'
# Or open any text editor from terminal
notepad hello.js # Windows
nano hello.js # Linux/macOS (if installed)
code hello.js # VS Code (if installed)
Step 3: Write Your First Script
Open hello.js in your editor and type:
// hello.js
// My first Node.js script
const greeting = "Hello, Node.js World!";
const currentTime = new Date().toLocaleTimeString();
console.log(greeting);
console.log("Current time:", currentTime);
function add(a, b) {
return a + b;
}
console.log("2 + 3 =", add(2, 3));
Save the file. This script:
- Declares variables
- Uses the built-in
Dateobject - Defines a function
- Prints output using
console.log
Running Script Using Node Command
The node Command
The node command followed by a filename tells Node.js to:
- Read that file
- Parse the JavaScript
- Execute every statement from top to bottom
- Exit when finished
Running Your First Script
Make sure you are in the directory containing hello.js:
cd my-node-project
node hello.js
Output:
Hello, Node.js World!
Current time: 2:15:30 PM
2 + 3 = 5
What happened:
- Node.js read
hello.js - It executed each line sequentially
-
console.logoutput appeared in your terminal - The process exited automatically when the file ended
Running Multiple Files
You can run any JavaScript file by passing its path to the node command:
node path/to/another-script.js
node ../parent-folder/script.js
node ./subfolder/nested.js
If no path is given (just node), the REPL starts instead.
Common node Command Options
| Command | What It Does |
|---|---|
node file.js |
Runs the specified file |
node |
Starts the interactive REPL |
node -e "code" |
Evaluates a string of code directly |
node --inspect file.js |
Runs file with debugging enabled |
Try the inline execution option:
node -e "console.log('Inline execution works!')"
Writing Hello World Server
What an HTTP Server Does
An HTTP server listens for incoming requests from browsers or other clients and sends back responses. When you visit a website, your browser sends an HTTP request; the server receives it and returns HTML, JSON, or other data.
Node.js has a built-in http module — no installation required, no frameworks needed.
Creating the Server File
Create a new file named server.js in your project directory:
// server.js
// A basic HTTP server using only Node.js built-in modules
const http = require('http');
// Create the server
const server = http.createServer((request, response) => {
// This function runs every time a request arrives
console.log(`Request received at ${new Date().toLocaleTimeString()}`);
console.log(`URL: ${request.url}`);
console.log(`Method: ${request.method}`);
// Set response headers
response.writeHead(200, {
'Content-Type': 'text/plain'
});
// Send response body
response.end('Hello, World! This is my first Node.js server.\n');
});
// Start listening for connections
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
console.log('Press Ctrl+C to stop the server');
});
Understanding the Code
| Line | What It Does |
|---|---|
const http = require('http'); |
Loads Node.js's built-in HTTP module |
http.createServer(...) |
Creates a server instance with a request handler function |
(request, response) => {...} |
The callback that runs for every incoming HTTP request |
response.writeHead(200, ...) |
Sets HTTP status to 200 (OK) and declares content type |
response.end(...) |
Sends the response body and closes the connection |
server.listen(PORT, ...) |
Tells the server to accept connections on port 3000 |
() => {...} in listen |
Callback that runs once the server starts successfully |
Running the Server
In your terminal:
node server.js
Output:
Server is running on http://localhost:3000
Press Ctrl+C to stop the server
The cursor stays blinking. The server is now running and waiting for requests. It will keep running until you stop it.
Testing the Server
Open your web browser and visit:
http://localhost:3000
You will see:
Hello, World! This is my first Node.js server.
Switch back to your terminal. You will see new log lines:
Request received at 2:18:45 PM
URL: /
Method: GET
Each browser visit triggers the server callback. Try refreshing the page multiple times and watch the terminal output.
Testing with curl (Terminal)
You can also test from another terminal window without opening a browser:
curl http://localhost:3000
Output:
Hello, World! This is my first Node.js server.
Or see the full HTTP response including headers:
curl -i http://localhost:3000
Output:
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Mon, 11 May 2026 14:20:00 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 48
Hello, World! This is my first Node.js server.
Stopping the Server
Return to the terminal running the server and press:
Ctrl + C
The server stops, and you return to your regular command prompt.
What You Have Built
Without installing any frameworks or libraries, you have:
- Installed Node.js — a complete JavaScript runtime on your machine
- Verified the installation — using terminal commands
- Used the REPL — an interactive environment for testing JavaScript
- Created a script file — saved JavaScript code to disk
-
Executed the script — ran it using the
nodecommand - Built an HTTP server — a working web server using only Node.js built-ins
File Structure
Your project should now look like this:
my-node-project/
├── hello.js # Your first script
├── server.js # Your first HTTP server
└── mysession.js # Saved REPL session (if you used .save)
Summary
| Task | Command / Action | What It Achieves |
|---|---|---|
| Install Node.js | Download from nodejs.org | Gets the JavaScript runtime on your computer |
| Check version | node --version |
Confirms installation worked |
| Open REPL | node |
Starts interactive JavaScript environment |
| Exit REPL |
.exit or Ctrl+C twice |
Returns to terminal |
| Create file | Any text editor | Saves code permanently |
| Run script | node filename.js |
Executes the file top-to-bottom |
| Start server | node server.js |
Runs a persistent HTTP listener |
| Stop server | Ctrl+C |
Terminates the running process |
Node.js gives you everything you need to run JavaScript out of the box: the runtime, the REPL, the module system, and built-in libraries for networking, file systems, and more. No browser required, no frameworks necessary. The http module you just used is only the beginning — Node.js has modules for encryption, compression, streams, child processes, and much more, all available with a simple require() call.
Remember: Every Node.js application starts the same way — a file, the
nodecommand, and JavaScript executing outside the browser. Frameworks like Express make things easier later, but understanding what Node.js does on its own is the foundation everything else builds upon.
Top comments (0)