Running your first Node.js program ("Hello World")
Running your first Node.js program is a great way to get started with Node.js, a JavaScript runtime that allows you to run JavaScript code on the server side. Below is a step-by-step guide to creating and running a simple "Hello World" program in Node.js.
1. Install Node.js
Before you can run Node.js programs, you need to install Node.js on your computer. You can download it from the official Node.js website.
2. Verify the Installation
After installing Node.js, you can verify the installation by opening your terminal or command prompt and typing the following commands:
node -v
This command will display the installed version of Node.js. You can also check the installed version of npm (Node Package Manager) with:
npm -v
3. Create Your First Node.js Program
Now, let's create a simple Node.js program that prints "Hello World" to the console.
-
Create a New File: Open a text editor and create a new file named
app.js
. -
Write the Code: Inside
app.js
, type the following code:console.log("Hello World");
This single line of JavaScript code uses
console.log()
to print "Hello World" to the console. -
Save the File: Save the file in a directory where you can easily access it from the terminal.
4. Run the Node.js Program
To run your Node.js program, follow these steps:
-
Open Terminal or Command Prompt: Navigate to the directory where you saved
app.js
. -
Run the Program: Type the following command:
node app.js
When you press Enter, Node.js will execute the code in
app.js
, and you should see the following output:Hello World
Explanation
-
console.log("Hello World");
: This line of code sends the string "Hello World" to the console (the terminal or command prompt where you ran the program). It's similar toprint()
in other programming languages. -
node app.js
: This command tells Node.js to run the code in theapp.js
file.
Example Output
When you run the program, your terminal should display:
Hello World
This output confirms that Node.js successfully ran your code, and your "Hello World" message was printed.
5. Additional Notes
-
No HTML or Browser Required: Unlike JavaScript in a web browser, Node.js runs on your server or local machine. You don't need an HTML file or a browser to run Node.js code.
-
Asynchronous Programming: Node.js is known for its asynchronous, non-blocking I/O model, which makes it efficient for building scalable network applications. However, this basic example is synchronous and runs in a single thread.
Summary
By following these steps, you've successfully written and executed your first Node.js program. This simple "Hello World" example is your entry point into the world of server-side JavaScript programming with Node.js. As you progress, you can explore more advanced topics such as modules, asynchronous programming, and building web servers with Node.js.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.