I am trying to get Node to prompt users for input using the readline module but I have some issues. I put the example from the readline documentation in a file called "prompt.js":
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
// TODO: Log the answer in a database
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
});
When I execute this command in the Windows console, everything works as expected:
node.exe prompt.js
However, with this command, which is the use-case that interests me, the prompt is displayed, but Node does not wait for user input and terminates immediately:
type prompt.js | node.exe
Upon investigating, I found that process.stdin.destroyed is true in this second case, before I get to use it.
I tried this example with a few Node versions (12, 14, 15.8.0) and all exhibit this problem. Any idea why piping to node fails? Is this a bug in Node or is there a rational explanation?