Lesson 3 of 50 – First Node.js Program
6%
6%

Your First Node.js Program

In this lesson, we will write our first JavaScript program and run it using Node.js in the terminal.

Node.js allows us to execute JavaScript code outside a web browser. We can create a JavaScript file, write code inside it, and execute that file using the node command.

Prerequisite: Make sure Node.js is installed on your computer. You can check it by running node -v in your terminal.

What Will We Learn?

Learning Objectives
  • Create a JavaScript file.
  • Write JavaScript code in the file.
  • Execute the file using Node.js.
  • Display messages using console.log().
  • Work with variables and basic calculations.

Step 1: Create a Project Folder

First, create a folder on your computer to store your Node.js programs.

Project Folder

Create a folder named:

MyNodeProject

You can create this folder on your Desktop or in any other convenient location.

Step 2: Create a JavaScript File

Open the project folder in a code editor such as Visual Studio Code. Create a new file named app.js.

Project Structure
MyNodeProject/
└── app.js
Remember: Save the file with the .js extension. Do not accidentally save it as app.js.txt.

Step 3: Write Your First Program

Write the following code inside the app.js file.

app.js
console.log("Hello, Node.js!");
Output
Terminal Output
Hello, Node.js!
Explanation
  • console provides methods for displaying messages and other information.
  • log() displays the supplied information in the terminal.
  • "Hello, Node.js!" is the text we want to display.
  • The semicolon marks the end of the statement; JavaScript does not always require it, but it is commonly used.

Step 4: Open the Terminal

To run your program, open a terminal in the project folder.

Using Visual Studio Code
  1. Open the MyNodeProject folder in VS Code.
  2. Click the Terminal menu.
  3. Select New Terminal.
  4. Make sure the terminal is opened in your project folder.

You can also use Command Prompt or another terminal, provided it is opened in the folder containing app.js.

Step 5: Run the Node.js Program

Type the following command in the terminal:

Run Command
node app.js
Output
Terminal
Hello, Node.js!
Congratulations! You have executed your first Node.js program.

Step 6: Display Multiple Messages

We can use multiple console.log() statements to display multiple lines of output.

Example 1: Multiple Messages
console.log("Welcome to Node.js");
console.log("I am learning JavaScript");
console.log("This is my first program");
Output
Welcome to Node.js
I am learning JavaScript
This is my first program

Step 7: Use Variables in Node.js

Variables store values that we can use in our programs. We can declare variables using let or const.

Example 2: Student Details
let name = "Rahul";
let age = 20;
let course = "Node.js";

console.log("Name:", name);
console.log("Age:", age);
console.log("Course:", course);
Output
Name: Rahul
Age: 20
Course: Node.js
Explanation
  • let name = "Rahul"; stores a name in a variable.
  • let age = 20; stores a number.
  • let course = "Node.js"; stores a course name.
  • console.log() displays the labels and variable values.

Step 8: Perform Calculations

Node.js can execute JavaScript arithmetic expressions just like a browser.

Example 3: Addition
let a = 10;
let b = 20;

let sum = a + b;

console.log("Sum:", sum);
Output
Sum: 30

Step 9: Display Your Name

Modify the following example to display your own name and city.

Example 4: Personal Information
const studentName = "Amit";
const city = "Aurangabad";

console.log("My name is " + studentName);
console.log("I live in " + city);
Output
My name is Amit
I live in Aurangabad
Try it yourself: Change "Amit" to your name and "Aurangabad" to your city. Save the file and run node app.js again.

Common Errors for Beginners

1. File Not Found

If Node.js cannot find app.js, check the file name and make sure the terminal is in the correct folder.

2. Incorrect File Extension

Make sure the file is named app.js, not app.js.txt.

3. Syntax Error

Incorrect code:

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

Correct code:

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

The opening and closing quotation marks must match.

Practical Assignment

Practice Tasks
  1. Create a file named app.js.
  2. Display your name using console.log().
  3. Display your age and city.
  4. Display three different messages on separate lines.
  5. Create two variables and display their sum.
  6. Display your course name using a variable.
  7. Run your program using node app.js.
💡 Remember
Write your JavaScript code in a .js file, save it, and use node filename.js to execute it.

🧠 Quiz

1. Which command runs a file named app.js?

2. Which function displays a message in the terminal?

3. Which file extension is commonly used for JavaScript files?