Node.js console based example
How To Write and Run Your First Program in Node.js
Learn to create, save, and run your first Node.js program using the console.log() function.
Create a new Node.js file
Create a new Node.js file and save it named console_first.js
→ File Name: console_first.js
console.log('Hello Node.js');
Open Command Prompt
Open Command Prompt or Node.js command prompt interface, depending on your operating system. For Windows users: Click on the start button and look for "Command Prompt" or simply write "cmd" in the search field.
Now that the command prompt is open, set your path. Here we have saved console_first.js file on the desktop.
→ Note: run the following code to navigate to desktop
cd desktop
Run the Program
After setting the path, run the following code to display output.
→ Note: For display output
node console_first.js
This code can be run on the command prompt or Node.js command prompt. You can see the output in the photos below.
a.) Command Prompt output
b.) Node.js Command Prompt output
Success!
Here you called the console.log() function and displayed a message on the console. This is your first Node.js program!