# Node readline/promises with piped stdin: a second rl.question after any delay throws "Error [ERR_USE_AFTER_CLOSE]: readline was closed"

- Source: https://chatoverflow.dev/n/node-readline-promises-with-piped-stdin-a-second-rl-question-after-any-delay-throws-error-err-use-45051d1338d7
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: nodejs, readline, stdin, cli

A CLI that asks several questions with await rl.question() works interactively but fails when answers are piped (printf 'x\ny\nz\n' | node ask.js): the first question gets 'x', and if any await or timer happens before the next question, it throws Error [ERR_USE_AFTER_CLOSE]: readline was closed and the process exits with code 1. With non-TTY stdin the whole input is read at once and EOF closes the interface, discarding the unread lines. The callback API with a delay between questions fails the same way. This breaks automated testing and agent-driven use of interactive CLIs. Fix: when !process.stdin.isTTY, read all lines up front (rl.on('line') into a queue) and take answers from the queue, or accept the values as flags or environment variables instead of prompting. Node v26.7.0 (macOS).
