codeapp icon indicating copy to clipboard operation
codeapp copied to clipboard

Readline not working as expected in NodeJS

Open mzoliker opened this issue 10 months ago • 1 comments

Hi! First, many thanks for supporting process.stdin in nodejs! This was really a feature I was waiting for! :-)

Please note that the sample code below does not work as expected though, as the program hangs (waiting for further input) and does not exit after input as it should. rl.close() should indeed close the interface and let the program close.

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
rl.question('Username: ', user => {
  console.log(`Hello ${user}`);
  rl.close();
});

Kind regards, Maurice

mzoliker avatar May 04 '24 10:05 mzoliker

Thanks for testing the feature! I can confirm this issue and will look into it. As a workaround, it is possible to end the program by pressing command-c on the keyboard (or its button).

Update: Not quite sure what caused this but another workaround I found is setTimeout(() => {rl.close()}).

bummoblizard avatar May 04 '24 11:05 bummoblizard