node js read input code example

Example 1: input in node js

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
 
readline.question('who are you: ', name => {
	console.log(`hello, hi there ${name}`);
	readline.close();
})

Example 2: readline in javascript

//Node.js readline
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What do you think of Node.js? ', (answer) => {
  console.log(`Thank you for your valuable feedback: ${answer}`);
  rl.close();
});

Example 3: node input

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 4: get input from user in nodejs

const prompt = require('prompt-sync')(); const name = prompt('What is your name?');console.log(`Hey there ${name}`);