Skip to main content

Command Palette

Search for a command to run...

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

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

Introduction:

Imagine you walk into a restaurant and place an order. Instead of waiting idle at the counter, you sit down, and the system processes your request in the background while handling other customers too.

That is exactly how Node.js works. It allows your computer to handle multiple tasks efficiently without blocking everything else.

Node.js is not just a tool, it is a JavaScript runtime environment that allows you to run JavaScript outside the browser.

Before building powerful apps, you need to set up your first Node.js environment properly. Let’s go step by step.

In this blog, we’ll break down the essentials of backend development by taking your JavaScript skills out of the browser and into the server with a clear, step-by-step guide to setting up your first Node.js application.

Installing Node.js

To get started, you need the runtime environment.

  • The Suggestion: Visit the official Node.js website. Always opt for the LTS (Long Term Support) version. It’s the most stable and reliable version for beginners and professionals alike.

  • OS-Neutral Tip: Whether you are on Windows, macOS, or Linux, the installer handles the path configurations for you. Just "Next, Next, Finish."

Checking Installation

Once installed, you need to verify that your system recognizes the node command. Open your terminal (Command Prompt, PowerShell, or Terminal.app) and type:

Command

node -v

If you see a version number (e.g., v20.10.0), you are ready to rock.

Also check npm (Node Package Manager):

npm -v

Understanding the Node REPL

Before we write files, let's look at the REPL.

REPL stands for Read-Eval-Print Loop. It is an interactive shell that takes your code, evaluates it, and prints the result immediately.

It means:

  • Read input

  • Evaluate code

  • Print output

  • Repeat

Start REPL

node

Example

> 2 + 3
5

> console.log("Hello Node")
Hello Node

Useful for quick testing and learning

It’s like a scratchpad for testing small snippets of logic without creating a full project. To enter it, simply type node in your terminal.

Creating Your First JS File

Now, let's move from the scratchpad to a permanent file.

  1. Create a folder named my-first-app.

  2. Inside, create a file named app.js.

  3. Add this simple line of code:

const greeting = "Hello, Node.js!";
console.log(greeting);

Running the Script

To execute your code, you use the node command followed by the filename.

Execution Flow:

[ Script (.js file) ]  -->  [ Node.js Runtime ]  -->  [ System Output ]
       |                          |                        |
   "app.js"             V8 Engine translates         "Hello, Node.js!"
                         JS to Machine Code

Writing a "Hello World" Server

To show the true power of Node.js, we will create a basic web server without using any frameworks (like Express). We use the built-in http module.

// Import the built-in HTTP module
const http = require('http');

// Configure the server to respond with "Hello World"
const server = http.createServer((req, res) => {
  res.statusCode = 200; // Success code
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World from my first server!');
});

// Tell the server to listen on port 3000
server.listen(3000, () => {
    console.log("Server running at http://localhost:3000");
});

How it works:

  1. require('http'): Pulls in the tools to handle networking.

  2. createServer: Sets up a "listener" that waits for someone to visit the URL.

  3. res.end: Sends the final message back to your browser.

Run it

node app.js

Open browser:

http://localhost:3000

Key Takeaways

  • V8 Engine: Node.js uses Google’s V8 engine to turn JS into fast machine code.

  • Non-Blocking: Node is designed to handle many tasks at once without waiting for one to finish before starting the next.

  • Verification: Always use node -v to ensure your environment is active.

  • REPL: Great for quick tests; Files are for building real applications.

In closing

I hope that you’ve found this blog on “Setting Up Your First Node.js Application Step-by-Step” helpful...!

That's all for today! 😁 You reached the end of the article 😍.

Want more..?

I write articles on princekumar-engineer.hashnode.dev, and also post development-related content on the following platforms:

Node.js

Part 3 of 15

A complete beginner-to-advanced guide to learning Node.js and Express.js, covering core concepts, asynchronous programming, REST APIs, middleware, file handling, and authentication techniques.

Up next

Blocking vs Non-Blocking Code in Node.js

Introduction: Imagine you’re at a tea stall: Blocking style → The shopkeeper serves only one customer at a time. Everyone else must wait. Non-blocking style → The shopkeeper takes multiple orders qu