Node.js Hello World: Write Your First Program

nodejs hello world

In this quick tutorial, you will start Node.js tutorials with a Hello World program. You will learn how to set up the environment and write your first code

Setting up Node.js Environment

You can install Node.js from the official site and pick the LTS version. You can also add npm, which comes inside Node.js, and you use it to manage packages.

So let’s check the installation by typing:

node -v

And

npm -v

These commands show the current version of Node.js and npm on your system.

You can create a new folder for your project and open it in your code editor. This folder holds your code and any files you add later.

Then, run this command to init the project:

npm init

#OR

npm init -y

This will create package.json file that is the map of the project that will store all modules you use.

Write Your First Hello World Program in Node.js

Inside the folder, make a new file named app.js. You write the code below inside that file.

console.log("Hello World");

Then, open the terminal inside the project folder and type:

node app.js

This command runs the file and prints Hello World on the screen.
You can change the text in the code and run it again to see a new output.

Create a Hello World Example for the Browser through Web Server

Inside the same folder, make a new file named server.js. Add the code below inside it.

const http = require("http");

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

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

This code starts a small server on port 3000 and sends Hello World as plain text to any browser.

Open a browser and go to http://localhost:3000 and you see the text Hello World from your Node.js server.

hello world app in http nodejs

Wrapping Up

You learned how to set up Node.js and create a folder for your first code, and run a Hello World program.

Here is a quick recap:

  • Install Node.js
  • Check the version
  • Make a folder for the project
  • Create a file with app.js
  • Write console.log
  • Run it with node.

FAQs

How do I write a Node.js Hello World program?


// Create a file named app.js
console.log("Hello World");
Steps:
  1. Install Node.js from official site
  2. Create a new file app.js
  3. Add console.log("Hello World");
  4. Run with node app.js

How do I run Node.js Hello World in terminal?


// Inside terminal
node app.js
Explanation:
  • Navigate to the folder where app.js is located
  • Run command node app.js
  • Output will display: Hello World

Why is Hello World important in Node.js?

  • It introduces you to the Node.js runtime
  • Shows how console.log outputs to terminal
  • Builds confidence for writing more complex programs

Can I create a Node.js Hello World web server?


const http = require('http');
http.createServer((req, res) => {
  res.write("Hello World");
  res.end();
}).listen(3000);
Steps:
  1. Create a file server.js
  2. Add the above code
  3. Run with node server.js
  4. Visit http://localhost:3000 to see Hello World

Similar Reads

Node.js HTTP Server with Examples for Beginners

This article shows how Node.js creates HTTP servers. It covers requests, responses, routes, HTML, JSON, and gives examples for both…

Node.js Modules in Depth with Examples

This guide shows Node.js modules, how they work, how to import and export them, and why you use them. What…

What is NodeJS? An Introduction to Node.js

What is Nodejs? Node.js is a powerful cross-platform JavaScript runtime that enables developers to execute JavaScript code on the server-side.…

Node.js File System with Examples

Node.js file system lets you read, write, update, and manage files and folders with built-in methods. Understand the Node.js File…

Installing Node js on Windows, Ubuntu, and MacOS

Installing node js on Windows, Ubuntu, or macOS. You need to pick the version of Node.js that you want to…

Node.js Streams Guide: How to Handle Data with Examples

Streams pass data in parts, so the app stays fast. This article explains Node.js Streams and how to use them.…

Node.js REPL: The Complete Tutorial for Beginners

Node.js REPL runs JavaScript code in an instant test space inside your terminal. What is Node.js REPL? Node.js REPL is…

Node Version Manager (NVM): Manage Node.js Versions

NVM (Node Version Manager ) is a command-line tool used to manage multiple versions of Node.js on a single machine.…

Node js NPM: How to Publish and Install JS Packages

Node Package Manager (NPM) is an essential tool at the heart of the Node.js ecosystem. This is the defacto package…

Node JS Buffers Tutorial for Beginners and Pros

Node JS Buffers store raw binary data in memory. This guide shows how to create them, work with them, and…

Previous Article

PHP array_intersect Function: How it Works with Examples

Next Article

React Components: A Complete Tutorial for Beginners

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.