Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createInterface prints double in terminal

when using the readline interface, everything from stdin gets printed twice to stdout:

var rl = require('readline');
var i = rl.createInterface(process.stdin, process.stdout);

when i run this code, everything i type in the terminal is duplicated. Typing 'hello world' yields:

hheelloo  wwoorrlldd

I guess it makes sense that it does this, since the readline module is meant to pipe an input to an output. But isnt it also meant to be used to create command line interfaces? I'm confused as to how im supposed to use it. Help?

like image 413
dopatraman Avatar asked Jul 09 '14 19:07

dopatraman


2 Answers

Try using terminal: false:

var readline = require("readline");
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
like image 185
0x17de Avatar answered Sep 22 '22 02:09

0x17de


I had this issue as well and I fixed it by ensuring that I only ever had one instance of a readline.interface at a time. I would recommend scoping the interface in the function that it is being used in so that once you leave that context it is cleaned up. Alternatively, you could simply declare a global instance that you use everywhere in your application. The underlying issue here is that when you have two instances (or more) listening to the same input stream (process.stdin) they will both pick up any input and they will both process it/ send it to the same output stream (process.stdout). This is why you are seeing double.

like image 30
kylestrader94 Avatar answered Sep 21 '22 02:09

kylestrader94