Readline and terminal interfaces
Prerequisite: node-vm-wasi-and-ffi-boundaries. Objective: Parse synthetic terminal input. Tested locally on Node.js 22.16.0 Linux. Each example is independent. Use a fresh disposable working directory, save as main.mjs, then run node main.mjs. The harness uses a five-second timeout and synthetic environment. These are static code blocks, not ByteMind interactive execution.
How it works
Use node:readline/promises or the readline async iterator for finite lines, and close the interface in finally. Input may come from a pipe instead of a terminal; test process.stdin.isTTY before prompting or adding terminal escape sequences. Keep machine-readable JSON on stdout and diagnostics on stderr. parseArgs supports typed string and boolean options, defaults and strict parsing. Validate business values after parsing: a string option is not automatically a valid numeric limit. node:repl serves interactive development, not untrusted input execution. ICU-backed Intl formatting depends on locale data; use explicit locale for human output and stable JSON for automation.
Parse synthetic lines without a terminal
import {createInterface} from 'node:readline/promises';import {Readable} from 'node:stream';const rl=createInterface({input:Readable.from(['tea\ncoffee\n\n']),terminal:false});const names=[];try{for await(const line of rl)if(line.trim())names.push(line.trim());}finally{rl.close();}console.log(JSON.stringify(names));Expected output:
["tea","coffee"]A synthetic stream supplies finite lines. Blank lines are excluded by an explicit application rule.
Validate typed CLI options and reject unknown flags
import {parseArgs} from 'node:util';import assert from 'node:assert/strict';const options={limit:{type:'string',default:'5'},json:{type:'boolean',default:false}};const {values}=parseArgs({args:['--limit','3','--json'],options});const n=Number(values.limit);assert.ok(Number.isInteger(n)&&n>=1&&n<=100);assert.throws(()=>parseArgs({args:['--bogus'],options}),{code:'ERR_PARSE_ARGS_UNKNOWN_OPTION'});console.log(JSON.stringify({limit:n,json:values.json}));Expected output:
{"limit":3,"json":true}Parsing identifies options; the subsequent range check enforces the application contract.
Common mistakes
Do not print prompts or color escapes into JSON output, and do not pass raw CLI arguments to a shell.
Practice
Design exit codes for invalid input and normal success, and test empty stdin, unknown flags and a non-TTY pipe.
Reference candidate: node-readline-and-terminal-interfaces-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/util.html (unverified); https://nodejs.org/download/release/v22.16.0/docs/api/readline.html (unverified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Readline and terminal interfaces — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Why check isTTY before printing a prompt?
Does parseArgs string type validate numeric bounds?
Where should machine-readable results go?