Hello World in Node.js
Node.js is a powerful runtime environment that allows us to build server-side applications using JavaScript. In this guide, we will walk you through the process of creating a simple "Hello World" program in Node.js.
Prerequisites
Before getting started with Node.js, make sure that you have it installed on your machine. If not, head over to the official Node.js website (https://nodejs.org/) and download the latest stable version suitable for your operating system.
Creating a New Project
To begin, open up your favorite code editor or integrated development environment (IDE). Create a new empty folder for our project and navigate into it using your command-line interface. To create our "Hello World" application file, follow these steps:
- Open a terminal or command prompt.
- Navigate to the directory where you want to create your project.
- Run the following command:
touch app.js
. This will create an empty JavaScript file named "app.js".
Writing Our "Hello World" Code
Now that we have our project set up with an empty JavaScript file named "app.js", let's write some code!
Open the app.js file in your preferred text editor and enter the following code:
console.log("Hello World!");
This simple line of code will output the phrase "Hello World!"
to the console when executed.
Save app.js by pressing Ctrl + S
(or Cmd + S
on macOS) and close it afterward.
Executing Our Program
To execute our program and see our "Hello World" message printed out to the console, follow these steps:
- Open up your terminal or command prompt.
- Navigate into the directory where you created app.js.
- Type
node app.js
in your terminal/command prompt and press Enter/Return.
You should now see the phrase "Hello World!"
printed out to your console. Congratulations, you have successfully executed your first Node.js program!
Conclusion
In this guide, we walked you through the process of creating a simple "Hello World" program in Node.js. We learned how to set up a new project, write our code, and execute it using the Node.js runtime environment.
Now that you have a basic understanding of how to create and run a simple Node.js program, you can explore more advanced topics like working with modules, building web servers, or developing scalable applications.
Happy coding!