Type the following command to check the Node.js version:
node -v
Type the following command to check the npm version:
npm -v
hello.js
.Write the following code in hello.js
:
console.log('Hello, World!');
hello.js
is located using the cd
command.Run the program with the following command:
node hello.js
You should see the output:
Hello, World!
You can try modifying the hello.js
file or creating new JavaScript files with the following examples:
Addition Program:
const a = 5;
const b = 10;
console.log('The sum is: ' + (a + b));
User Input Program (using the readline
module):
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('What is your name? ', (name) => {
console.log(`Hello, ${name}!`);
readline.close();
});
Output:
Hello, World!
The sum is: 15
What is your name? Gautam Ankoji
Hello, Gautam Ankoji!