DEV Community

Harman Panwar
Harman Panwar

Posted on

Setting Up Your First Node.js Application Step-by-Step

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:

  1. Double-click the downloaded .msi file
  2. Follow the installation wizard (accept defaults)
  3. The installer adds Node.js to your system PATH automatically

On macOS:

  1. Double-click the downloaded .pkg file
  2. Follow the installation wizard
  3. Alternatively, if you use Homebrew: brew install node

On Linux:

  1. The downloaded file is typically a tarball or package
  2. Or use your package manager:
    • Ubuntu/Debian: sudo apt install nodejs npm
    • Fedora: sudo dnf install nodejs
    • Arch: sudo pacman -S nodejs npm

Step 3: Verify the Installation

Open your terminal (or command prompt on Windows) and run:

node --version
Enter fullscreen mode Exit fullscreen mode

You should see output similar to:

v20.12.1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

10.5.0
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

  1. Reads the code you type
  2. Evaluates (executes) that code
  3. Prints the result back to you
  4. 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
Enter fullscreen mode Exit fullscreen mode

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.
>
Enter fullscreen mode Exit fullscreen mode

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 ]
Enter fullscreen mode Exit fullscreen mode

What you observed:

  • Arithmetic works immediately
  • String concatenation returns the combined string
  • Variables can be declared with let and inspected by name
  • console.log prints to the terminal, then returns undefined
  • 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
Enter fullscreen mode Exit fullscreen mode

Exiting the REPL

You can exit in three ways:

  • Type .exit and press Enter
  • Press Ctrl + C twice
  • 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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

Save the file. This script:

  • Declares variables
  • Uses the built-in Date object
  • 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:

  1. Read that file
  2. Parse the JavaScript
  3. Execute every statement from top to bottom
  4. Exit when finished

Running Your First Script

Make sure you are in the directory containing hello.js:

cd my-node-project
node hello.js
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, Node.js World!
Current time: 2:15:30 PM
2 + 3 = 5
Enter fullscreen mode Exit fullscreen mode

What happened:

  • Node.js read hello.js
  • It executed each line sequentially
  • console.log output 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
Enter fullscreen mode Exit fullscreen mode

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!')"
Enter fullscreen mode Exit fullscreen mode

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');
});
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

Server is running on http://localhost:3000
Press Ctrl+C to stop the server
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

You will see:

Hello, World! This is my first Node.js server.
Enter fullscreen mode Exit fullscreen mode

Switch back to your terminal. You will see new log lines:

Request received at 2:18:45 PM
URL: /
Method: GET
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, World! This is my first Node.js server.
Enter fullscreen mode Exit fullscreen mode

Or see the full HTTP response including headers:

curl -i http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Stopping the Server

Return to the terminal running the server and press:

Ctrl + C
Enter fullscreen mode Exit fullscreen mode

The server stops, and you return to your regular command prompt.


What You Have Built

Without installing any frameworks or libraries, you have:

  1. Installed Node.js — a complete JavaScript runtime on your machine
  2. Verified the installation — using terminal commands
  3. Used the REPL — an interactive environment for testing JavaScript
  4. Created a script file — saved JavaScript code to disk
  5. Executed the script — ran it using the node command
  6. 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)
Enter fullscreen mode Exit fullscreen mode

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 node command, 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)