Paramclasses Academy - Blog

Tutorials Details

←
Post 4 of 40
→

How to Create Your First Node.js Application Step by Step

How to Create Your First Node.js Application Step by Step

October 29, 2026

✦ How to Create Your First Node.js Application Step by Step


✦ Introduction

Node.js is a popular JavaScript runtime environment used to build web servers, backend applications, REST APIs, and automation tools. If you are new to backend development, creating your first Node.js application is a great way to understand how JavaScript works outside a browser.

You do not need advanced programming knowledge to get started. With a basic understanding of JavaScript, you can create a simple application, execute it from the terminal, and gradually add more functionality.

In this tutorial, Param Classes explains how to set up a Node.js project, write your first program, use npm, create a basic HTTP server, and understand common errors beginners may encounter.

✦ What Do You Need Before Starting?

Before creating your first application, make sure you have the following:

  • Node.js: Runs JavaScript outside the browser.

  • npm: Helps manage packages and project dependencies.

  • Code editor: Visual Studio Code or another editor.

  • Terminal: Command Prompt, PowerShell, or the terminal inside your editor.

Download a supported Node.js LTS version from the official website:

https://nodejs.org/

After installation, open your terminal and run:

node -v
npm -v

Both commands should display version numbers. If they do, you are ready to create your first project.

✦ Step 1: Create a Project Folder

First, create a folder for your application.

You can create a folder named:

my-first-node-app

Open this folder in Visual Studio Code or your preferred editor.

Alternatively, create and open the folder from a terminal.

mkdir my-first-node-app
cd my-first-node-app

The mkdir command creates a new directory, while cd moves the terminal into that directory.

Keeping project files inside a dedicated folder makes your application easier to manage.

✦ Step 2: Create Your First JavaScript File

Inside the project folder, create a file named:

app.js

Add the following code:

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

const studentName = "Param Classes";

console.log("Welcome to " + studentName);

This is a simple Node.js program.

The console.log() function displays information in the terminal. The const keyword declares a variable whose binding cannot be reassigned.

Here, studentName stores the text "Param Classes".

✦ Step 3: Run Your First Node.js Application

Open the terminal inside your project folder and run:

node app.js

Expected output:

Hello, Node.js!
Welcome to Param Classes

Congratulations! You have created and executed your first Node.js application.

You can change the text in app.js, save the file, and run the command again to see the updated output.

✦ Step 4: Understand Variables and Functions

As you develop larger applications, you will use variables and functions to organize your code.

Consider this example:

const name = "Student";

function greetUser(userName) {
    return "Welcome, " + userName + "!";
}

const message = greetUser(name);

console.log(message);

Output:

Welcome, Student!

In this example:

  • name stores the user's name.

  • greetUser() is a function that accepts a parameter.

  • return sends a value back from the function.

  • message stores the returned result.

Understanding these fundamentals will make it easier to learn Node.js modules, APIs, and backend logic.

✦ Step 5: Initialize the Project with npm

npm can help you manage your application's configuration and dependencies.

Inside the project directory, run:

npm init -y

This command creates a package.json file using default settings.

The file contains information about your project, such as its name, version, scripts, and dependencies.

A basic package.json may look like this:

{
  "name": "my-first-node-app",
  "version": "1.0.0",
  "description": "My first Node.js application",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  }
}

Your generated file may contain additional fields or slightly different values.

Now you can run the application using npm:

npm start

Because the start script executes node app.js, it runs the same program as before.

✦ Step 6: Create a Basic HTTP Server

Node.js can also create a server that responds to browser requests.

Create a new file named server.js and add this code:

const http = require("node:http");

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

    res.end("Hello! Welcome to my Node.js server.");
});

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

Let's understand the important parts.

require("node:http"): Loads Node.js's built-in HTTP module.

http.createServer(): Creates an HTTP server and registers a request handler.

req: Represents the incoming request.

res: Represents the response sent to the client.

res.writeHead(): Sets the HTTP status code and response headers.

res.end(): Completes the response.

server.listen(3000): Starts listening on port 3000.

✦ Step 7: Test Your Node.js Server

Save server.js and execute:

node server.js

Your terminal should display:

Server running at http://localhost:3000

Open your browser and visit:

http://localhost:3000

You should see:

Hello! Welcome to my Node.js server.

Your browser sends an HTTP request, and the Node.js server responds with text.

Keep the terminal running while testing. To stop the server, press Ctrl + C.

✦ Step 8: Organize Your Project

As your project grows, you can organize related files into separate folders.

For example:

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

Later, you might add folders for routes, controllers, database configuration, and reusable modules.

Organizing files helps make larger applications easier to understand and maintain.

✦ Common Mistakes Beginners Should Avoid

1. Using the wrong folder: Make sure your terminal is inside the project directory before running node app.js.

2. Incorrect file names: Verify that the file is named app.js, not app.js.txt.

3. Node.js command not recognized: Check the installation and PATH configuration, then reopen the terminal.

4. Syntax errors: Check brackets, quotation marks, commas, and variable names.

5. Port already in use: If port 3000 is occupied, stop the other server or change the port in server.js.

6. Stopping the server accidentally: Keep the terminal session open while testing your HTTP server.

Debugging these issues is a normal part of learning programming.

✦ What Should You Learn Next?

After creating your first application, continue learning these topics:

  • Node.js built-in modules.

  • JavaScript callbacks, Promises, and async/await.

  • File handling with the fs module.

  • Creating HTTP servers.

  • npm packages and dependencies.

  • Express.js and REST API development.

  • MySQL or MongoDB database integration.

  • Authentication, testing, and deployment.

Param Classes recommends practising each topic with small programs before combining them into a complete project.

✦ Frequently Asked Questions (FAQs)

1. Is Node.js suitable for beginners?

Yes. Beginners can start with simple JavaScript programs and gradually learn server development.

2. Which file extension is used for Node.js programs?

The .js extension is commonly used for JavaScript files executed by Node.js.

3. How do I run a Node.js file?

Open a terminal in the file's directory and run node filename.js.

4. What does npm init -y do?

It creates a package.json file with default project configuration.

5. Can Node.js create a web server?

Yes. Its built-in HTTP module can create a server that receives requests and sends responses.

6. Do I need Express.js for my first Node.js application?

No. You can begin with Node.js built-in features and learn Express.js when you are ready to build more structured web applications.

✦ Conclusion

Creating your first Node.js application is an important step toward backend development. You have learned how to create a project folder, execute JavaScript, initialize npm, and build a basic HTTP server.

Continue practising these examples and gradually explore modules, asynchronous programming, Express.js, and databases.

Param Classes supports learners in building their programming knowledge through structured learning and practical development exercises.

Website: https://paramclasses.in/

←
Post 4 of 40
→