Skip to main content

Command Palette

Search for a command to run...

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

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

Node.js is a popular JavaScript runtime environment that allows developers to run JavaScript outside the browser. It is widely used for building web servers, APIs, and backend applications.

In this blog, we will learn how to install Node.js, verify the installation, use the Node REPL, create our first JavaScript file, and build a simple Hello World server.

What Is Node.js?

Node.js is a runtime environment that executes JavaScript code on our computer or server.

Before Node.js, JavaScript was mainly used inside web browsers. With Node.js, JavaScript can be used to create backend applications and server-side programs.

Installing Node.js

The easiest way to install Node.js is by downloading it from the official website.

Steps:

  1. Visit the Node.js website.

  2. Download the recommended LTS (Long-Term Support) version.

  3. Run the installer.

  4. Complete the installation process.

The installer automatically installs:

  • Node.js Runtime

  • npm (Node Package Manager)

After installation, open your terminal or command prompt.

Checking the Installation

To verify that Node.js is installed correctly, run:

node -v

Example output:

v24.0.0

You can also check npm:

npm -v

Example output:

10.5.0

If version numbers appear, Node.js has been installed successfully.

Understanding the Node REPL

Before writing files, it is useful to learn about the Node REPL.

REPL stands for:

  • Read

  • Evaluate

  • Print

  • Loop

It is an interactive environment where you can execute JavaScript code directly from the terminal.

Starting the REPL

Open your terminal and type:

node

You will see something like:

>

Now you can write JavaScript code.

Example:

2 + 3

Output:

5

Another example:

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

Output:

Hello Node.js

To exit the REPL:

exit

or press:

Ctrl + C twice

The REPL is useful for testing JavaScript quickly without creating files.

Creating First JavaScript File

Create a new file named:

app.js

Add the following code on that file:

console.log("Hello World");

Save the file.

Running a JavaScript File with Node.js

Open terminal in the folder where the file is located.

Run:

node app.js

Output:

Hello World

Congratulations! You have executed your first Node.js program.

How Node Executes a Script

When you run:

node app.js

Node.js:

  1. Reads the file.

  2. Executes the JavaScript code.

  3. Displays the output in the terminal.

Creating a Hello World Server

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

Create a file named:

server.js

Add the following code:

import http from 'node:http'

const server = http.createServer((req, res) => {
    res.end('Hello World');
});

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

Save the file.

Running the Server

Execute:

node server.js

Output:

Server running on port 3000

Now open your browser and visit:

http://localhost:3000

You will see:

Hello World

Your first Node.js web server is now running.

Conclusion

Node.js makes it easy to run JavaScript outside the browser. After installing Node.js, you can verify the setup, experiment with code using the REPL, create JavaScript files, and run them directly from the terminal. You can even build a simple web server using only Node.js core modules. These basics provide a strong foundation for learning backend development with Node.js.