
Node.js is a powerful, efficient, and scalable JavaScript runtime built on Chrome’s V8 engine. It’s widely used for building server-side applications, APIs, and more. If you’re just getting started with Node.js, writing a simple “Hello World” program is the perfect way to dip your toes into this exciting technology. In this tutorial, we’ll walk you through the steps to write a “Hello World” program in Node.js, using both CommonJS and ES Modules syntax, suitable for the latest versions of Node.js.
Prerequisites
Before we begin, ensure the following:
- Node.js Installed: Download and install Node.js from the official website. Use the latest LTS version for stability.
- Text Editor: Any text editor will do, but we recommend Visual Studio Code for a great developer experience.
Writing "Hello World" in Node.js
Node.js allows us to create web servers using its built-in HTTP module. Let’s see how to set this up step by step.
Step 1: Create a New File
Create a new file called app.js (or any name you prefer) in your project folder.
Step 2: Write the Code
Here’s how to write your first “Hello World” program in Node.js:
Using CommonJS (Default Syntax)
// Import the HTTP module
const http = require('http');
// Create an HTTP server
const server = http.createServer((req, res) => {
// Set the response header
res.writeHead(200, { 'Content-Type': 'text/plain' });
// Send the response body
res.end('Hello World\n');
});
// Start the server
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Using ES Modules (Modern Syntax)
If you prefer ES Modules, supported in the latest Node.js versions, the code looks like this:
// Import the HTTP module
import http from 'http';
// Create an HTTP server
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
});
// Start the server
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Step 3: Run the Program
- Open a terminal in your project directory.
- Run the script using the Node.js command:
node app.js
Open your browser to visit:
http://localhost:3000/
You should see the message “Hello World” displayed.
Key Differences Between CommonJS and ES Modules
- CommonJS is the default module system in Node.js. It uses require to import modules.
- ES Modules (ECMAScript Modules) use the import syntax and are supported in modern Node.js versions.
- To use ES Modules, you need to:
- Save your file with a .mjs extension, OR
- Add “type”: “module” in your package.json file.
Common Errors
1. Port Already in Use: If you get an error like EADDRINUSE, it means another process is using port 3000. Change the port number in your code.
const PORT = 4000;
2. Syntax Errors: Ensure you’re using the correct syntax for your module type (CommonJS or ES Modules).
Next Step: What to Learn After This
Now that you’ve successfully created a basic server, here are some suggestions on what to learn next:
- Build a Simple API: Learn how to create RESTful APIs using Node.js and serve JSON data to clients.
- Work with Frameworks: Explore popular frameworks like Express.js to simplify server creation and routing.
- Connect to a Database: Understand how to use databases such as MongoDB, MySQL, or PostgreSQL with Node.js.
- Environment Variables: Learn to manage environment-specific configurations using tools like dotenv.
- Error Handling: Dive into best practices for handling errors in your Node.js applications.
- Asynchronous Programming: Master asynchronous programming concepts like promises and async/await to handle non-blocking operations efficiently.
Each of these topics will help you build a strong foundation in Node.js and prepare you for real-world application development.

Your First “Hello, World!” in Angular
