What does the node:readline module provide, and what is a common use case for it?
Short answer
readline reads input from a readable stream (commonly process.stdin) line by line -- the standard way to build interactive command-line prompts, asking the user a question and waiting for their response without manually buffering stdin.
Advertisement
AModel answer
readline reads input from a readable stream (commonly process.stdin) line by line -- the standard way to build interactive command-line prompts, asking the user a question and waiting for their response without manually buffering stdin.
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question('What is your name? ', (name) => { console.log(`Hello, ${name}!`); rl.close(); });Real-world example
A database migration CLI uses readline to prompt 'This will delete data, continue? (y/n)' before a destructive operation, giving the user a chance to cancel.
Likely follow-ups
- How does readline/promises simplify usage with async/await compared to the callback-based version?
- How would you build a multi-step interactive CLI wizard using repeated readline.question() calls?
Related topics