DEV Community

Cover image for Setting Up Your First Node.js Application
Satpalsinh Rana
Satpalsinh Rana

Posted on

Setting Up Your First Node.js Application

No frameworks. No boilerplate generators. No "clone this starter repo." Just you, a terminal, and Node.js starting from nothing and understanding every single step.

This guide works on Windows, macOS, and Linux. The concepts are the same everywhere.

Step 1: Installing Node.js

Go to nodejs.org and you'll see two versions available:

  • LTS (Long-Term Support) — Stable, recommended for most projects and beginners
  • Current — Latest features, but less battle-tested

Install the LTS version. Unless you specifically need a bleeding-edge feature, LTS is the right call. It's what production systems use.

The installer on the site handles everything — Node.js itself, and npm (Node Package Manager) which comes bundled with it. Just run the installer and follow the prompts.

Linux users: You can install via your package manager (apt, dnf, etc.), but the versions in default repos are often outdated. Use the installer from nodejs.org or a version manager like nvm for better control.

Pro tip most tutorials skip: If you're going to work on multiple Node.js projects over time — especially ones that need different Node versions — install nvm (Node Version Manager) instead. It lets you switch between Node versions per-project. It's worth the five extra minutes to set up. Check github.com/nvm-sh/nvm for macOS/Linux, or github.com/coreybutler/nvm-windows for Windows.

Step 2: Verifying the Installation

Open your terminal (Command Prompt, PowerShell, Terminal, or bash — doesn't matter) and run:

node --version
Enter fullscreen mode Exit fullscreen mode

You should see something like:

v20.11.0
Enter fullscreen mode Exit fullscreen mode

Then check npm:

npm --version
Enter fullscreen mode Exit fullscreen mode
10.2.4
Enter fullscreen mode Exit fullscreen mode

The exact numbers will differ based on when you're reading this. What matters is that both commands return a version without an error. If you see command not found or similar, the installation didn't complete successfully — try reinstalling.

What's actually happening here? When you type node, your operating system looks through a list of directories (your PATH) for a program called node. The installer added Node's directory to your PATH automatically. --version is just a flag that tells the program to print its version and exit.

Step 3: Understanding the Node REPL

Before you write any files, let's talk about the REPL — because most tutorials throw you into it without explaining what it actually is.

REPL stands for Read-Eval-Print Loop.

  • Read — it reads what you type
  • Eval — it evaluates (runs) it as JavaScript
  • Print — it prints the result
  • Loop — it does this over and over until you quit

Think of it like a calculator for JavaScript. You type an expression, hit Enter, and get the result immediately. No files, no compilation, no running scripts.

To open the REPL, just run:

node
Enter fullscreen mode Exit fullscreen mode

Your prompt changes to >, which means you're inside the REPL. Try this:

> 2 + 2
4

> "hello" + " world"
'hello world'

> const x = 10
undefined

> x * 3
30

> [1, 2, 3].map(n => n * 2)
[ 2, 4, 6 ]
Enter fullscreen mode Exit fullscreen mode

The REPL is great for:

  • Testing a snippet of code quickly
  • Exploring how a built-in function behaves
  • Trying out a data transformation before writing it in a file

To exit the REPL, press Ctrl + C twice, or type .exit.

What most developers don't realize: The REPL has a .help command that shows you a list of special dot-commands. There's .load to load a JS file into the REPL session, .save to save your session history to a file, and .editor to enter multi-line edit mode. Niche but occasionally very handy.

Step 4: Creating Your First JavaScript File

Exit the REPL and create a folder for your project:

mkdir my-first-node-app
cd my-first-node-app
Enter fullscreen mode Exit fullscreen mode

Now create a file. You can use any text editor — VS Code, Sublime, Notepad, vim, whatever you have. Create a file called index.js with this content:

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);

const now = new Date();
console.log("Current time:", now.toLocaleTimeString());
Enter fullscreen mode Exit fullscreen mode

Save it. That's it — that's a valid Node.js program.

One thing worth knowing: Node.js and browser JavaScript share the same language (ES2015+ works in both), but they have different environments. In the browser, you have window, document, localStorage, and the DOM. In Node.js, you have process, __filename, __dirname, the fs module, and access to the operating system. You'll never accidentally use document.getElementById in a Node script — it simply doesn't exist there.


Step 5: Running Your Script

In the terminal, from inside your project folder, run:

node index.js
Enter fullscreen mode Exit fullscreen mode

You should see:

Hello from Node.js!
Doubled: [ 2, 4, 6 ]
Current time: 10:45:22 AM
Enter fullscreen mode Exit fullscreen mode

That's it. That's how you run a Node.js script.

Let's break down what happened:

  1. You invoked the node runtime
  2. You told it which file to execute (index.js)
  3. Node read the file, compiled it with V8, and executed it top to bottom
  4. Output went to your terminal via console.log
  5. Node exited when the script finished

What most tutorials don't mention about console.log: In Node.js, console.log writes to stdout (standard output). There's also console.error which writes to stderr — this is important when you start building scripts that need to separate normal output from errors, or when you're chaining commands in bash. Same syntax, different stream.

Also: you can run node -e "console.log('hello')" to run a one-line script without a file at all. Handy for quick tests.

Step 6: Writing Your First HTTP Server

Here's where it gets interesting. Node.js ships with a built-in http module. You don't need Express, Fastify, or any other framework for this — just pure Node.

Create a new file called server.js:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  console.log(`Request received: ${req.method} ${req.url}`);

  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World from Node.js!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
Enter fullscreen mode Exit fullscreen mode

Run it:

node server.js
Enter fullscreen mode Exit fullscreen mode

You'll see:

Server running at http://127.0.0.1:3000/
Enter fullscreen mode Exit fullscreen mode

Now open your browser and navigate to http://localhost:3000. You should see "Hello World from Node.js!" in the browser.

Back in your terminal, you'll also see the log line printed each time a request comes in.

To stop the server, press Ctrl + C.

What's Actually Happening in That Server Code

Let's walk through it because there's more here than meets the eye.

require('http') — This is how Node.js imports built-in modules (and later, npm packages). The http module is part of Node's standard library; no installation needed.

http.createServer(callback) — This creates an HTTP server. The callback function runs every single time a request arrives. It receives two objects: req (the incoming request — method, URL, headers, body) and res (the outgoing response — you control the status code, headers, and body).

res.end() — This sends the response and closes the connection. Without this, the browser would hang waiting for a response that never comes.

server.listen(port, hostname, callback) — This tells Node to start listening for connections on the specified address and port. The callback runs once when the server is successfully bound and ready to accept requests.

What most tutorials don't explain about the port: 3000 is a convention for development. Ports below 1024 are "privileged" on Unix-based systems — you'd need admin rights to use port 80 (HTTP) or 443 (HTTPS) directly. In development, you run on a high port like 3000 or 8080, and in production a reverse proxy (like Nginx) handles the traffic on port 80/443 and forwards it to your app. This is why you'll almost never see a production Node app actually binding to port 80.

A Quick Upgrade: Routing by URL

The basic server returns the same response for every request. Here's how you can handle different routes without any framework:

const http = require('http');

const server = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/plain');

  if (req.url === '/' && req.method === 'GET') {
    res.statusCode = 200;
    res.end('Welcome to the home page!\n');
  } else if (req.url === '/about' && req.method === 'GET') {
    res.statusCode = 200;
    res.end('This is the about page.\n');
  } else {
    res.statusCode = 404;
    res.end('404 - Page not found\n');
  }
});

server.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
Enter fullscreen mode Exit fullscreen mode

This is exactly the kind of routing logic that frameworks like Express abstract away. Understanding how it works at this level makes you a significantly better developer when you eventually do use a framework — you're not guessing at the magic, you've seen the underlying mechanism.


The process Object — Something Worth Knowing

Node.js gives you a global process object that exposes a lot of useful runtime information. Add this to any script and run it:

console.log("Node version:", process.version);
console.log("Platform:", process.platform);
console.log("Working directory:", process.cwd());
console.log("Script arguments:", process.argv);
Enter fullscreen mode Exit fullscreen mode

process.argv is particularly useful — it contains all the command-line arguments passed to your script. process.argv[0] is the path to node, process.argv[1] is the path to your script, and process.argv[2] onward are the arguments you passed.

This is how you'd build simple CLI tools in Node without any library.

Quick Reference: Commands You'll Use Daily

# Run a script
node filename.js

# Open the REPL
node

# Check Node version
node --version

# Check npm version
npm --version

# Initialize a new project (creates package.json)
npm init -y

# Install a package
npm install express

# Run a script defined in package.json
npm run start
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
tahosin profile image
S M Tahosin

Nice intro for anyone just getting started with Node. One thing that helped me early on was using nodemon during development so you don't have to restart the server manually every time you change something. Small thing but saves a lot of frustration. Keep writing!