Hi friends, in this tutorial, you will learn how to run or execute your first node js hello world program. If you are wondering how to do this, then you have come to the right place. Before getting started with node js, let us discuss node js a little bit.
What is node js and why node js
Node js is open source and provides a server-side environment which means it used javascript on the server side instead of client-side and all the files are declared with .js extension. Node js can handle multiple requests at once and return the responses at the same time but other scripting languages such as PHP can not take multiple requests from the client machines simultaneously.
Also, read, the Node JS HTTP server example
Node js programs can be executed in two ways as given below.
By using the command line interface i.e. cmd
By using the vs code editor
Steps to run the node js hello world program using the command line interface
Step 1:- download node js from here and install it in your system.
Step 2:- Open the command line interface in your system.
Step 3:- Type node and press ENTER to check whether node js is properly installed in your system in CLI.
Step 4:- you will see the output as shown below
C:\Users\Lenovo>node
Welcome to Node.js v18.16.0.
Type ".help" for more information.
\>
if you see the above information then you are confirmed that node js is installed and you can debug or print anything you want with the help of the console class. Note that the javascript console is different from than console provided by the node.
Now, you can run the node js hello world program as shown below
console.log("Hello World");
Hello World
undefined
Note that log is a function used with the console class provided by node js. As soon as you write anything inside the log() function and press ENTER, it will print the value inside the log() function. Undefined is shown just to let you know that log() does not return anything.
Also read, How to Import Custom Module in Node JS
Using the vs code editor
First of all, download and install the vs code editor.
Now, create a file inside the vs code editor with .js extension as shown below
Now, open the js file you created and write "Hello World" inside console.log() and save the file using CTRL+S.
Now open the terminal inside the vs code editor as shown below
In the terminal, type node filename and press ENTER.
It will display the output as Hello World as shown below.
Conclusion:- I hope this tutorial will help you to understand the concept. If you have any doubts then please leave a comment below.