18

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?

dopatraman
  • 13,416
  • 29
  • 90
  • 154

3 Answers3

42

Try using terminal: false:

var readline = require("readline");
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
0x17de
  • 615
  • 6
  • 6
  • F.H. you still can use backspace then. per default your line should be buffered by your terminal and not sent until you press return – 0x17de Jul 22 '18 at 13:18
  • 3
    this solves the issue but the terminal capabilities like using tab and arrow keys are lost – Ehsan Shekari May 19 '19 at 04:12
8

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.

kylestrader94
  • 83
  • 1
  • 4
-2

You should be using the options object format:

var i = rl.createInterface({
  input: process.stdin,
  output: process.stdout
});
glortho
  • 13,120
  • 8
  • 49
  • 45