Loading lesson path
The Readline module is a core Node.js module that provides an interface for reading data from a Readable stream (like process.stdin ) one line at a time.
Formula
REPL (Read - Eval - Print Loop) environmentsThe Readline module is built into Node.js, so no additional installation is required. It's perfect for any application that needs to interact with users through the command line or process text input in a line-oriented way.
Formula
Here's a quick example of using the Readline module to create a simple interactive command - line application:Basic Interactive Prompt const readline = require('readline');
// Create interface for input/output const rl = readline.createInterface({input: process.stdin, output: process.stdout
});
// Ask a question and handle the response rl.question('What is your name? ', (name) => {
console.log(`Hello, ${name}!`);
// Ask follow-up question rl.question('How old are you? ', (age) => {
console.log(`In 5 years, you'll be ${parseInt(age) + 5} years old.`);
// Close the interface when done rl.close();
});
});
// Handle application exit rl.on('close', () => {
console.log('Goodbye!');
process.exit(0);
});The Readline module can be imported in several ways depending on your module system and needs: CommonJS (Node.js default)
// Basic require const readline = require('readline');
// Import specific methods using destructuring const { createInterface } = require('readline');
// Create interface with default settings const rl = createInterface({input: process.stdin, output: process.stdout
});
ES Modules (Node.js 12+)
// Using default import import readline from 'readline';
// Using named imports import { createInterface } from 'readline';
// Dynamic import (Node.js 14+)
const { createInterface } = await import('readline');
// Create interface const rl = createInterface({input: process.stdin, output: process.stdout
});Always close the readline interface using rl.close() when you're done with it to free up system resources and allow your program to exit cleanly.
The createInterface method is the main way to create a readline interface. It takes an options object with several configuration properties:
Basic Interface Creation const readline = require('readline');
// Create a basic interface const rl = readline.createInterface({
input: process.stdin, // Readable stream to listen to output: process.stdout, // Writable stream to write to prompt: '> ', // Prompt to display (default: '> ')
});input : The Readable stream to listen to (default: process.stdin )
: The Writable stream to write to (default: process.stdout ) prompt : The prompt string to use (default: '> ') terminal : If true, treats the output as a TTY (default: output.isTTY ) historySize : Maximum number of history entries (default: 30) removeHistoryDuplicates : If true, removes duplicate history entries (default: false) completer
Formula
: Optional function for tab auto - completion crlfDelay: Delay between CR and LF (default: 100ms) escapeCodeTimeout : Time to wait for character escape sequences (default: 500ms)
const readline = require('readline');
const fs = require('fs');
// Create an interface with advanced options const rl = readline.createInterface({
input: fs.createReadStream('input.txt'), // Read from file output: process.stdout, // Write to console terminal: false, // Input is not a terminal historySize: 100, // Larger history removeHistoryDuplicates: true, // Don't store duplicate commands prompt: 'CLI> ', // Custom prompt crlfDelay: Infinity, // Handle all CR/LF as single line break escapeCodeTimeout: 200 // Faster escape code detection
});
// Handle each line from the file rl.on('line', (line) => {
console.log(`Processing: ${line}`);
});
// Handle end of file rl.on('close', () => {
console.log('Finished processing file');
});When creating interfaces for file processing, set terminal: false to disable TTY-specific features and improve performance.