Skip to main content

Command Palette

Search for a command to run...

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

Updated
9 min read
Setting Up Your First Node.js Application Step-by-Step
R

Software engineer passionate about tech, innovation & research. I explore, build, and share insights on coding, systems, and emerging technologies.

Introduction to Node.js

Node.js Official Website is a free, open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript outside the browser.

Before Node.js existed, JavaScript was mainly used for frontend development inside web browsers. With Node.js, developers can build backend applications, APIs, command-line tools, and real-time servers using JavaScript.

Node.js was originally created by Ryan Dahl in 2009.

One of the biggest reasons Node.js became popular is that developers can now use a single programming language for both frontend and backend development.

What Node.js Actually Is

Node.js is not:

  • A programming language

  • A framework

  • A database

Node.js is a runtime environment.

It uses Google's V8 JavaScript Engine to execute JavaScript code outside the browser.

The V8 engine compiles JavaScript into machine code, making execution extremely fast.

This allows developers to:

  • Build servers

  • Create APIs

  • Handle files

  • Work with databases

  • Build networking applications

using JavaScript alone.

Why Developers Use Node.js

Node.js is widely used because it offers:

  • Fast execution using the V8 engine

  • Event-driven architecture

  • Non-blocking I/O operations

  • Cross-platform support

  • Large npm ecosystem

  • Easy backend development with JavaScript

Common use cases include:

  • REST APIs

  • Real-time chat applications

  • Streaming services

  • Backend systems

  • Command-line tools

Installing Node.js

Before starting your first Node.js application, you must install Node.js on your system.

Requirements:

  • A computer running Windows, macOS, or Linux

  • Internet connection

  • Terminal or command prompt access

Visit the official Node.js download page:

Node.js Download Page

The website automatically detects your operating system.

Understanding LTS vs Current Version

When downloading Node.js, you will usually see two versions:

Version Type Description
LTS (Long Term Support) Stable and recommended for most users
Current Latest features but less stable

For beginners and production applications, use the LTS version.

Installing Node.js on Windows

Step 1: Download Installer

Download the .msi installer from the official Node.js website.

Step 2: Run Installer

Double-click the downloaded installer file.

Step 3: Follow Installation Wizard

Continue through:

  • Next

  • Accept License Agreement

  • Install

  • Finish

The installer automatically installs:

  • Node.js

  • npm (Node Package Manager)

Installing Node.js on macOS

Step 1: Download Installer

Download the .pkg installer.

Step 2: Open Package

Run the installer package.

Step 3: Follow Setup Instructions

Complete the installation process using the setup wizard.

Node.js and npm are installed together automatically.

Installing Node.js on Linux

The recommended way to install Node.js on Linux is using NVM (Node Version Manager).

NVM allows developers to:

  • Install multiple Node.js versions

  • Switch versions easily

  • Avoid sudo permission issues

Install NVM

Run:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash

Reload Shell

source ~/.bashrc

or restart the terminal.

Install Latest LTS Version

nvm install --lts

Checking Node.js Installation

After installation, verify Node.js using the terminal.

Check Node Version

node -v

or:

node --version

Example output:

v24.3.0

This confirms Node.js is installed successfully.

Checking npm Installation

npm stands for Node Package Manager.

It is bundled automatically with Node.js.

Check npm version:

npm -v

Example:

10.5.0

What npm Is

npm is the package manager for Node.js.

It allows developers to:

  • Install libraries

  • Manage dependencies

  • Download open-source packages

  • Run scripts

Example:

npm install express

This installs the Express.js framework.

Understanding the Node.js REPL

REPL stands for:

Read → Evaluate → Print → Loop

The Node.js REPL is an interactive environment where you can run JavaScript code line-by-line directly inside the terminal.

It is useful for:

  • Testing code quickly

  • Debugging

  • Learning JavaScript

  • Trying small snippets

Starting the Node REPL

Open terminal and run:

node

You will enter interactive mode.

Example:

>

Now you can write JavaScript directly.

Using the REPL

Basic Math

> 2 + 2
4

Variables

> const name = "Node"
undefined
> name
'Node'

Console Output

> console.log("Hello World")
Hello World
undefined

Exiting the REPL

Use:

.exit

or press:

Ctrl + C

twice.

Running JavaScript Without Creating Files

Node.js allows direct execution using the -e flag.

Example:

node -e "console.log('Hello from Node.js')"

Output:

Hello from Node.js

This is useful for:

  • Quick testing

  • One-line scripts

  • Small automation tasks

Creating Your First JavaScript File

Node.js applications are usually written inside .js files.

Create a file:

index.js

You can create it using:

  • VS Code

  • Any text editor

  • Terminal

Create File Using Terminal

Linux/macOS:

touch index.js

Windows PowerShell:

New-Item index.js

Writing Your First Node.js Program

Open index.js and add:

console.log("Hello from Node.js");

Running JavaScript Files Using Node.js

Use the node command followed by the filename.

Example:

node index.js

Output:

Hello from Node.js

This is the basic workflow of Node.js:

  1. Write JavaScript

  2. Save file

  3. Execute using node

Understanding How Node Executes Files

When you run:

node index.js

Node.js:

  1. Reads the file

  2. Parses JavaScript

  3. Executes code using the V8 engine

  4. Displays output in terminal

Unlike browsers, Node.js has access to:

  • File system

  • Operating system APIs

  • Networking

  • Process management

Creating Your First Node.js Server

One of the most important uses of Node.js is creating web servers.

Node.js includes a built-in HTTP module that allows developers to create servers without installing external libraries.

Understanding the HTTP Module

The http module is a built-in Node.js library used for:

  • Creating web servers

  • Handling requests

  • Sending responses

  • Managing networking operations

Import it using:

const http = require("http");

Writing a Basic Hello World Server

Create a file:

server.js

Add the following code:

const http = require("http");

const server = http.createServer((req, res) => {

  res.statusCode = 200;

  res.setHeader("Content-Type", "text/plain");

  res.end("Hello World from Node.js");
});

const PORT = 3000;

server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}`);
});

Understanding the Server Code

Importing HTTP Module

const http = require("http");

Loads Node's built-in HTTP functionality.

Creating the Server

const server = http.createServer((req, res) => {

});

Creates a web server.

The callback runs every time a request reaches the server.

Understanding req and res

req (Request Object)

Contains incoming request information:

  • URL

  • Headers

  • HTTP method

  • Query parameters

res (Response Object)

Used to send data back to the client.

Setting Response Status Code

res.statusCode = 200;

200 means successful response.

Setting Response Headers

res.setHeader("Content-Type", "text/plain");

Tells browser the response type.

Sending Response

res.end("Hello World from Node.js");

Ends the response and sends data to client.

Starting the Server

server.listen(PORT, () => {

});

Starts listening for incoming requests.

Running the Server

Execute:

node server.js

Output:

Server running at http://localhost:3000

Viewing Server Output in Browser

Open browser and visit:

http://localhost:3000

You will see:

Hello World from Node.js

Understanding What Happens Internally

When browser visits:

http://localhost:3000

The following happens:

  1. Browser sends HTTP request

  2. Node.js server receives request

  3. createServer() callback executes

  4. Server sends response

  5. Browser displays response

Common Beginner Mistakes

Forgetting to Save File

Always save the .js file before running Node.js.

Running Wrong Filename

Incorrect:

node app.js

when file is actually:

index.js

Port Already in Use

Error:

EADDRINUSE

Means another application is already using that port.

Change:

const PORT = 3001;

Missing Node Installation

If terminal says:

node: command not found

then Node.js is not installed correctly.

Understanding Project Structure

Basic beginner project:

my-first-node-app/
│
├── index.js
├── server.js
└── package.json

What package.json Is

package.json stores project metadata:

  • Project name

  • Version

  • Dependencies

  • Scripts

Create it using:

npm init -y

Why Node.js Is Important

Node.js allows JavaScript developers to move beyond browsers.

With Node.js you can build:

  • APIs

  • Backend servers

  • Real-time systems

  • Command-line applications

  • Streaming platforms

  • Automation scripts

using one language.

Best Practices for Beginners

Use LTS Version

Avoid unstable current releases while learning.

Learn Core Node.js First

Understand:

  • HTTP module

  • File system

  • Event loop

  • Modules

before learning frameworks.

Practice Terminal Commands

Node.js development heavily uses terminal workflows.

Keep Files Organized

Separate:

  • Server code

  • Utilities

  • Configurations

as projects grow.

Avoid Frameworks Initially

Learning pure Node.js first helps understand how backend systems actually work internally.

Summary

In this guide, we explored how to set up your first Node.js application step-by-step.

We covered:

  • What Node.js is

  • Installing Node.js

  • Verifying installation

  • Using npm

  • Understanding Node REPL

  • Running JavaScript files

  • Creating a basic server

  • Understanding HTTP requests and responses

You also created your first Node.js web server using only the built-in HTTP module.

Understanding these fundamentals provides the foundation for learning:

  • Express.js

  • APIs

  • Databases

  • Authentication

  • Real-time applications

  • Full-stack JavaScript development

Node.js becomes much easier once you understand how JavaScript executes outside the browser and how servers respond to incoming requests.