The Readline Module in Node.js

The readline module in Node.js provides an interface for reading data from a Readable stream (such as process.stdin) line by line. It is widely used for building command-line interfaces (CLIs), handling user input, and processing files line-by-line. Node.js supports both Promise-based APIs and Callback-based APIs for working with readline.

readline.Interface / readlinePromises.Interfaceline, close, pause, resume, SIGINT, SIGTSTPrl.question()rl.prompt(), rl.setPrompt(), rl.getPrompt()rl.write(), rl.cursorTo(), rl.moveCursor()

~2 min read · Updated Dec 30, 2025

1. Introduction


The readline module allows developers to read input line by line from streams. It can be loaded with require('node:readline') or the Promise-based version require('node:readline/promises').


2. Creating an Interface


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

3. Key Events


  • line: Triggered when a line of input is received.
  • close: Triggered when the interface is closed.
  • pause / resume: Manage input stream state.
  • SIGINT: Triggered by Ctrl+C.
  • SIGTSTP / SIGCONT: Handle backgrounding and resuming processes.
  • history: Triggered when the input history changes.

4. Core Methods


  • rl.question(query, callback): Displays a query and captures user input.
  • rl.prompt(): Displays the prompt and waits for input.
  • rl.setPrompt(prompt): Sets the prompt text.
  • rl.getPrompt(): Returns the current prompt.
  • rl.write(data[, key]): Writes data or simulates key input.
  • rl.close(): Closes the interface.

5. Async Iteration


The readline interface supports for await...of loops for asynchronous line-by-line processing:


for await (const line of rl) {
  console.log(`Received: ${line}`);
}

6. Example Use Cases


  • Simple CLI: Build interactive command-line tools with custom commands.
  • File Processing: Read files line-by-line using fs.createReadStream() with readline.

7. TTY Keybindings


The readline module supports keybindings such as Ctrl+C, Ctrl+D, Ctrl+U, and Ctrl+K for managing input in terminal sessions.


Conclusion


The readline module is a powerful tool for building interactive applications in Node.js. With support for both Promise-based and Callback-based APIs, rich event handling, and file processing capabilities, it is essential for developing CLIs and data-processing utilities.


Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue