QA
Beginner LevelNode JS

How do you build interactive CLI prompts using the node:readline module?

❓ Interview Prompt: Explain `node:readline` module, `createInterface({ input, output })`, prompt questions, and closing interfaces.

The 'node:readline' module reads line-by-line input from readable streams (like 'process.stdin'):

Promises API ('node:readline/promises'):

Provides 'async/await' prompt capabilities for building CLI tools!

Code Implementation Example

javascript
import readline from 'node:readline/promises';
import { stdin as input, stdout as output } from 'node:process';

async function askUser() {
  const rl = readline.createInterface({ input, output });
  const name = await rl.question('What is your name? ');
  console.log('Hello, ${name}!');
  rl.close();
}
askUser();
💡Building interactive CLI prompts using node:readline/promises.
🎯 Interview Key Takeaway

Use node:readline/promises for building interactive command-line interfaces with async/await.

#readline#CLI#Interactive Prompts#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?