nodejs take input from console code example
Example 1: promp node js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("What is your name ? ", function saveInput(name) {
console.log(`His name is ${name}`);
rl.close();
});
rl.on("close", function saveInput() {
console.log("\nBYE BYE !!!");
process.exit(0);
});
Example 2: how to get input from user in nodejs
const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout}); readline.question('Who are you?', name => { console.log(`Hey there ${name}!`); readline.close();});
Example 3: get input from user in nodejs
const prompt = require('prompt-sync')(); const name = prompt('What is your name?');console.log(`Hey there ${name}`);