Introduction
When learning a new programming language or technology, the first step is usually to create a simple program that displays a message. In Node.js, the traditional first program is called Hello World.
Node.js allows developers to execute JavaScript outside a web browser. This means you can write JavaScript code and run it directly on your computer using the Node.js runtime.
In this tutorial, you will learn how to create your first Node.js program, run a JavaScript file, display output in the terminal, and understand how the program works.
What Is a Node.js Hello World Program?
A Node.js Hello World program is a simple JavaScript program that displays the message Hello World! in the terminal.
It helps beginners understand the basic process of writing, saving, and executing JavaScript code using Node.js.
Unlike JavaScript code that runs inside a web browser, a Node.js program can run directly from the command line without requiring a browser.
For example:
console.log("Hello World!");
When you execute this program using Node.js, the output will be:
Hello World!
Prerequisites
Before creating your first Node.js program, make sure you have:
- Node.js installed on your computer.
- A code editor, such as Visual Studio Code or Notepad.
- Basic knowledge of JavaScript.
You can download Node.js from the official website:
https://nodejs.org/
After installing Node.js, you can check whether it is available on your system.
Open a terminal or command prompt and run:
node -v
This command displays the installed Node.js version.
Next, check the npm version:
npm -v
npm is the package manager commonly installed with Node.js. You do not need npm to run a basic Hello World program, but it is useful for managing packages in later projects.
Step 1: Create a JavaScript File
First, create a folder for your Node.js program.
For example, create a folder named node-hello-world.
Open the folder in your code editor and create a new file named hello.js.
Your project structure should look like this:
node-hello-world/
└── hello.js
The .js extension indicates that the file contains JavaScript code.
Step 2: Write the Hello World Program
Open hello.js and add the following code:
console.log("Hello World!");
Save the file.
This is your first Node.js program. The console.log() method displays the specified message in the terminal when the program runs.
You can also write a more descriptive message:
console.log("Hello World!");
console.log("Welcome to Node.js.");
console.log("This is my first Node.js program.");
This program contains three statements, each displaying a separate message.
Step 3: Run the Node.js Program
To execute the program, open a terminal in the directory containing hello.js.
Run the following command:
node hello.js
The output will be:
Hello World!
Welcome to Node.js.
This is my first Node.js program.
Node.js reads and executes the JavaScript code in the specified file.
Important: Run the command from the correct directory, or provide the appropriate path to the JavaScript file.
Step 4: Understand the Hello World Code
Let’s understand the statement used in the program:
console.log("Hello World!");
| Code | Description |
|---|---|
| console | Provides access to console-related methods. |
| log() | Displays information in the console or terminal. |
| “Hello World! | The string that will be displayed. |
| ; | Ends the statement; JavaScript can also insert semicolons automatically in many cases. |
When Node.js executes this statement, the message is written to standard output, which is usually visible in your terminal.
Step 5: Create a Hello World Program Using a Variable
You can also store the message in a variable before displaying it.
Example:
const message = "Hello World!";
console.log(message);
Output:
Here, const declares a variable whose binding cannot be reassigned. The variable message stores the string, and console.log(message) displays its value.
This example introduces a basic JavaScript concept that you will use frequently in Node.js applications.
Step 6: Create a Hello World Program Using a Function
You can place the message-printing logic inside a function.
Example:
function sayHello() {
console.log("Hello World!");
}
sayHello();
Output:
In this example:
- function sayHello() defines a function named sayHello.
- console.log() displays the message when the function executes.
- sayHello() calls the function.
Functions help organize code into reusable blocks. As your Node.js programs become larger, you can use functions to separate different tasks.
Step 7: Display Dynamic Output in Node.js
Node.js programs can also display values dynamically.
For example:
const name = "Rahul";
console.log(`Hello, ${name}!`);
Output:
The backticks create a JavaScript template literal. The expression ${name} inserts the variable’s value into the string.
You can use the same technique to display user names, product details, application status messages, and other dynamic information.
Step 8: Create a Node.js Hello World Web Server
A basic Hello World program displays output in the terminal. Node.js can also create a web server that responds to browser requests.
The following example uses Node.js’s built-in http module:
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, {
"Content-Type": "text/plain; charset=utf-8"
});
res.end("Hello World!");
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
Save this code in a file named server.js.
Run the program:
node server.js
You should see:
Server running at http://localhost:3000
Open the following address in your web browser:
http://localhost:3000
The browser will display:
Hello World!
How Does the Web Server Work?
- require(“http”) imports Node.js’s built-in HTTP module.
- http.createServer() creates an HTTP server.
- 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() sends the response body and completes the response.
- server.listen(3000) starts listening on port 3000.
This example demonstrates the difference between printing a message in the terminal and returning a message from a web server.
To stop the server, press Ctrl + C in the terminal.
Common Mistakes When Running a Node.js Program
Beginners sometimes encounter errors when running their first program. The following table explains common issues and their solutions.
| Problem | Solution |
|---|---|
| node is not recognized | Install Node.js and check that its installation directory is available in your system’s PATH. |
| Cannot find the file | Make sure the filename and directory are correct. |
| Using the wrong filename | Run node hello.js if your file is named hello.js. |
| Program output does not appear | Save the file and check that the program contains a console.log() statement. |
| Port 3000 is already in use | Stop the other process using the port or choose another available port. |
| Browser cannot open the local server | Make sure the server is running and visit the correct localhost URL. |
Advantages of Starting with a Hello World Program
A Hello World program is simple, but it helps beginners understand several important concepts:
- Basic JavaScript syntax: You learn how to write a statement and use a string.
- Program execution: You learn how to execute a JavaScript file using Node.js.
- Terminal usage: You become familiar with running commands from the command line.
- Output handling: You learn how to display information using console.log().
- Foundation for larger applications: You can build on this example to learn variables, functions, modules, file operations, and web servers.
Conclusion
The Node.js Hello World program is a simple starting point for learning server-side JavaScript. By creating a JavaScript file and running it with the node command, you can display messages directly in the terminal.
You also learned how to use variables and functions and how to create a basic HTTP web server that returns a Hello World message to a browser.
Once you understand these fundamentals, you can continue learning Node.js modules, file handling, npm packages, and web application development.