const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What is your name? ', (name) => {
console.log(`Hello, ${name}!`);
rl.close();
});
- Import the
readline
module from the Node.js standard library. - Create a Readline interface using
readline.createInterface
with process.stdin
as the input stream and process.stdout
as the output stream. - Prompt the user with the question 'What is your name? ' using
rl.question
. - Handle the user's input in the callback function, where the provided
name
is printed to the console along with a greeting. - Close the Readline interface using
rl.close()
to free up resources and end the program.