Skip to main content

Command Palette

Search for a command to run...

Setting Up Your First Node.js Application

Updated
5 min read
Setting Up Your First Node.js Application

Modern web development depends heavily on JavaScript, and one of the biggest reasons for that is Node.js. It allows developers to run JavaScript outside the browser, making it possible to build servers, APIs, command-line tools, automation systems, and real-time applications using a single language.

In this article, you will learn how to install Node.js.

What is Node.js?

Node.js is a JavaScript runtime built on Chrome’s V8 engine. Instead of running JavaScript only inside browsers, Node.js allows JavaScript code to run directly on your operating system.

This means JavaScript can now:

  • Read and write files

  • Create servers

  • Handle databases

  • Build APIs

  • Run backend applications

One of the biggest advantages of Node.js is that developers can use JavaScript for both frontend and backend development.

Installing Node.js

To start using Node.js, you first need to install it on your computer.

You can download it from the official website:

Node.js Official Website

You will usually see two versions:

  • LTS (Long Term Support) → Recommended for most users

  • Current → Latest features but may be less stable

For beginners, the LTS version is the best choice.

The installation process is almost identical across operating systems:

  1. Download the installer

  2. Run the installer

  3. Follow the setup instructions

  4. Finish installation

The installer automatically adds Node.js to your system path, which allows you to use the node command from the terminal.

Checking Installation Using Terminal

After installation, open your terminal or command prompt and run:

node -v

You should see something like:

v24.1.0

This confirms Node.js is installed successfully.

You can also verify npm installation:

npm -v

npm stands for Node Package Manager. It is included automatically with Node.js and is used to install libraries and tools.

Understanding the Node REPL

Before creating files, it is important to understand the REPL.

REPL stands for:

  • Read

  • Evaluate

  • Print

  • Loop

It is an interactive environment where Node.js reads your JavaScript code, executes it, prints the result, and waits for the next command.

You can start the REPL by typing:

node

After running this command, you will see something like:

>

Now you can write JavaScript directly.

Example - 1 :

> 2 + 3
5

Example - 2 :

> const name = "Node"
undefined

> name
'Node'

The REPL is extremely useful for:

  • Testing JavaScript quickly

  • Learning syntax

  • Debugging small code snippets

  • Experimenting with Node features

To exit the REPL:

.exit

Or press

Ctrl + c

Creating Your First JavaScript File

Now let’s create an actual JavaScript file.

Step - 1 : Create a folder in your computer

my-first-node-app

Step - 2 : Inside that folder, create a file named, with .js extension

app.js

Step - 3 : Add the following code

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

Running Script Using Node Command

Open your terminal inside the project folder and run

node app.js

Output

Hello from Node.js

How Node Executes Your Script

Here is the simplified execution flow

JavaScript File -> Node Runtime -> V8 Engine -> Output

Explanation

JavaScript File

This contains the code you write.

Node Runtime

Node.js loads and manages the execution process.

V8 Engine

The V8 engine converts JavaScript into machine code.

Output

The result appears in the terminal or server response.

Writing Your First Hello World Server

Let's try to create a Node.js server without installing extra frameworks

Create a new file

server.js

Add this code to that file

const http = require("http");

const server = http.createServer((req, res) => {
  res.writeHead(200, {
    "Content-Type": "text/plain",
  });

  res.end("Hello World");
});

server.listen(3000, () => {
  console.log("Server running on port 3000");
});

Let's understand the server code

Importing the HTTP Module

const http = require("http");

Node.js includes built-in modules. The http module helps create servers.

Creating the Server

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

This creates a server that listens for incoming requests.

  • req → Request object

  • res → Response object

Sending Response Headers

res.writeHead(200, {
  "Content-Type": "text/plain",
});

This tells the browser:

  • Status code is 200 (success)

  • Response type is plain text

Sending Response Body

res.end("Hello World");

This sends the final response to the browser.

Starting the Server

server.listen(3000, () => {
  console.log("Server running on port 3000");
});

The server starts running on port 3000.

Running the Server

Run the file

node server.js

You would see

Server running on port 3000

Now open your browser and visit

http://localhost:3000

You will see

Hello World

Congratulations your first server is ready !!!

Why This Matters

This small example introduces the core idea behind backend development:

  • A client sends a request

  • The server processes it

  • The server sends a response

Almost every modern web application works using this pattern.