Ankaj Gupta
December 25, 2018

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.

1

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');
2

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.

Command Prompt search
Node.js search

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
3

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

Command Prompt output

b.) Node.js Command Prompt output

Node.js console output

Success!

Here you called the console.log() function and displayed a message on the console. This is your first Node.js program!

Node.js

Join the discussion

Comments

0 comments

No comments yet — be the first to share your thoughts.

Related Posts