Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

readline

Node.js readline provides line-by-line access to a Readable stream, including process.stdin. Its promise-based API supports interactive questions with async/await.

How it works

The node:readline module reads a Readable stream one line at a time. Create an Interface with input and optional output streams, then consume lines or use the promise API to ask questions; close the interface when interaction is complete.

Syntax

readline.createInterface({ input, output })

Parameters

NameTypeDescription
inputReadableReadable stream supplying input, commonly process.stdin.
outputWritableWritable stream used to display prompts and terminal output.

Returns

An Interface instance for line-oriented input and interaction.

Key points

  • Readline turns stream input into line-oriented interaction, which is useful for command-line programs.
  • The promise-oriented variant is available through the node:readline/promises entry point.
  • CommonJS applications can load the promise API with require.
  • ES module applications can import the promise API as a namespace.

Common mistakes

  • The interface consumes a Readable stream, so closing it after use is necessary for short-lived interactive programs.
  • Use node:readline/promises when you want question prompts to return promises instead of using callbacks.

Example: Ask one terminal question

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

const rl = readline.createInterface({ input, output });
const answer = await rl.question('What is your name? ');
console.log(`Hello, ${answer}`);
rl.close();

Creates a promise-based terminal interface, waits for one answer, prints a greeting, and closes the interface.

When to use it

CLI prompts

Build interactive command-line utilities that collect user responses.

Line-oriented stream processing

Process incoming text streams one complete line at a time.

Promise-based interaction

Use async/await for sequential terminal questions.

Interview questions

QWhat problem does the readline module solve?

Use createInterface with a Readable input stream and, for terminal interaction, a writable output stream. The resulting Interface supports line-oriented reading and question prompts.

Follow-up: How would you avoid callback nesting? Use the promise-based node:readline/promises API and await question calls.

QHow do you ask a user a question asynchronously?

The promise API is imported from node:readline/promises. Its question method displays a query and resolves with the user's response, allowing sequential async control flow.

Follow-up: What must happen after the final question? Close the interface so it no longer keeps the process waiting for input.

QHow is a terminal readline interface configured?

Pass process.stdin as input and process.stdout as output when creating the interface. After interaction, call close on the returned Interface.

Follow-up: Can the output stream be omitted? Yes; an interface can be configured without terminal output when output is not needed.

  • readline/promises
  • readline.createInterface
  • Interface
  • process.stdin
  • process.stdout

Source: Readline. Checked 2026-10-15.