Node.js First Program

Every programmer starts with a small program that prints a message on the screen. You will create a file, write one line of code, and run it from the terminal.

What You Build

You build a program that prints the text Hello, Node.js! in the terminal. The program is tiny, yet it proves that your Node.js setup works.

Create and Run the Program

Step 1: Make a Project Folder

Open the terminal and create a folder for your practice files.

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

Step 2: Create the File

Create a file named hello.js inside the folder. Open it in any text editor, such as Visual Studio Code or Notepad.

Step 3: Write the Code

Type this line in the file and save it.

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

Step 4: Run the File

Type the following command in the terminal.

node hello.js

The terminal prints this output.

Hello, Node.js!

How Node.js Runs Your File

Think of Node.js as a translator. You write instructions in JavaScript, and the computer needs machine instructions. Node.js reads your file and translates each line as it runs.

  hello.js            node command           Terminal
+-------------+     +--------------+     +-----------------+
| console.log |     |  Reads the   |     |                 |
| ("Hello")   | --> |  file and    | --> | Hello, Node.js! |
|             |     |  runs it     |     |                 |
+-------------+     +--------------+     +-----------------+
   Your code          The translator        Your result

Understand Each Part

console

The console is a built-in object. It connects your program to the terminal screen.

log

The log method sends a message to the console. Think of it as a person reading your note aloud.

The Text in Quotes

Quotes mark a piece of text. JavaScript calls this a string. You can use double quotes or single quotes.

The Semicolon

The semicolon ends the statement. It works like the full stop at the end of a sentence.

Make the Program Smarter

Add variables and a function to make the program more useful. A variable stores a value. A function groups instructions under one name.

const name = "Asha";

function greet(person) {
  return "Welcome, " + person + "!";
}

console.log(greet(name));
console.log("Today is " + new Date().toDateString());

Run it with node hello.js. The terminal prints a welcome message and the current date.

Read Words from the Command Line

Node.js lets you pass words to your program when you run it. The process.argv array holds those words.

const userName = process.argv[2];
console.log("Hello, " + userName);

Run the file like this.

node hello.js Ravi

The output is Hello, Ravi. The array stores the node path at position 0 and the file path at position 1. Your own word sits at position 2.

process.argv
+-----------+-----------------+----------+
| index 0   | index 1         | index 2  |
| node path | hello.js path   | "Ravi"   |
+-----------+-----------------+----------+

Fix Common Errors

node is not recognized

This message means your computer cannot find Node.js. Install Node.js again and restart the terminal.

Cannot find module

This message means Node.js cannot find your file. Check the file name and confirm the terminal sits in the same folder as the file.

Unexpected token

This message points to a typing mistake. Look for a missing quote, bracket, or parenthesis near the line number in the error.

Practice Tasks

  • Print your name and your city on two separate lines.
  • Store two numbers in variables and print their sum.
  • Write a function that returns the square of a number and print the result for 4.
  • Pass your age as a command-line word and print it with a message.

Small experiments build confidence. Change the code, run it, and read the output each time.