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
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
Welcome to Node.js v18.16.0.
Type “.help” for more information.
Now, you can run the node js hello world program as shown below
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.