A Nodejs “Hello World” program is a simple program that displays the message “Hello, World!” on the screen. It is often used as a basic introduction to a programming language or platform.
Node.js is a free, open-source, cross-platform JavaScript runtime environment that allows you to build scalable and high-performance applications using JavaScript on the server side. It uses an event-driven, non-blocking I/O model, which makes it efficient and suitable for real-time applications.
To install Node.js on your computer, you can follow these general steps:
Windows:
- Go to the official Node.js website (https://nodejs.org/en/download/) and download the Windows installer for the current version of Node.js.
- Double-click the downloaded installer file and follow the prompts to install Node.js.
- Open the command prompt by pressing
Win + R
and typingcmd
, then press Enter. - Verify that Node.js and npm are installed by running the following commands:
node -v
npm -v
macOS:
- Go to the official Node.js website (https://nodejs.org/en/download/) and download the macOS installer for the current version of Node.js.
- Double-click the downloaded installer file and follow the prompts to install Node.js.
- Open the Terminal app by going to Applications > Utilities > Terminal.
- Verify that Node.js and npm are installed by running the following commands:
node -v
npm -v
Linux:
The installation steps for Node.js and npm may vary depending on your Linux distribution. Here are the general steps:
- Open a terminal window.
- Install Node.js and npm using your distribution package manager. For example, on Ubuntu or Debian, you can run the following command:
sudo apt-get install nodejs npm
On Fedora or CentOS, you can run the following command:
sudo dnf install nodejs npm
3. Verify that Node.js and npm are installed by running the following commands:
node -v
npm -v
NodeJs Hello World example
Simple example code.
1. Create a new file called app.js
in a directory of your choice.
2. In the app.js
file, add the following code:
console.log("Hello, World!");
3. Open a terminal or command prompt in the directory where the app.js
file is located.
4. Run the following command to execute the app.js
file:
node app.js
Output:
Do comment if you have any doubts or suggestions on this Basic and first code of Node js topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version