Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node readline module doesn't have 'on' function?

I'm trying to create a node app that reads a textfile line by line using the 'readline' module, and prints it to the console.

  var lineReader = require('readline');
  lineReader.createInterface({
    input: fs.createReadStream('./testfile')
  });
  lineReader.on('line', function(line){
    console.log(line);
  });

According to the module's documentation, there should be an 'on' method. However when I log the instance of the readline object I created, I don't see an 'on' method anywhere:

{ createInterface: [Function],   Interface:    { [Function: Interface]
     super_:
      { [Function: EventEmitter]
        EventEmitter: [Circular],
        usingDomains: false,
        defaultMaxListeners: [Getter/Setter],
        init: [Function],
        listenerCount: [Function] } },   
emitKeypressEvents: [Function: emitKeypressEvents],   
cursorTo: [Function: cursorTo],   
moveCursor: [Function: moveCursor],   
clearLine: [Function: clearLine],   
clearScreenDown: [Function: clearScreenDown],   
codePointAt: [Function: deprecated],   
getStringWidth: [Function: deprecated],   
isFullWidthCodePoint: [Function: deprecated],   
stripVTControlCharacters: [Function: deprecated] }

And so, naturally, when I call lineReader.on(), I get an error saying the function doesn't exist.

I'm following the documentation precisely... what am I missing? Where is the on method?

Thank you so much in advance for your time.

like image 345
SemperCallide Avatar asked Apr 18 '17 13:04

SemperCallide


People also ask

What is readline module in Node JS?

Readline Module in Node.js allows the reading of input stream line by line. This module wraps up the process standard output and process standard input objects. Readline module makes it easier for input and reading the output given by the user.

How do I use the'readline'module?

To use this module, do require ('readline'). Readline allows reading of a stream (such as process.stdin) on a line-by-line basis. Note that once you've invoked this module, your node program will not terminate until you've closed the interface.

How to receive user input using readline module in Java?

To receive user input using readline module, you need to create an Interface instance that is connected to an input stream. You create the Interface using readline.createInterface () method, while passing the input and output options as an object argument. Here’s an example of creating the readline interface:

How to read file content using readline module in Python?

To read file content using readline module, you need to create the same interface using createInterface () command, but instead of connecting the input option to the process.stdin object, you connect it to the fs.createReadStream () method.


1 Answers

Keep reading the docs until you find an example with context:

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

rl.setPrompt('OHAI> ');
rl.prompt();

rl.on('line', function(line) {
  switch(line.trim()) {
  // …

on is a method of the interface returned by the createInterface method, not of the readline module itself.

  var lineReader = require('readline');

  // You need to capture the return value here
  var foo = lineReader.createInterface({
    input: fs.createReadStream('./testfile')
  });

  // … and then use **that**
  foo.on('line', function(line){
    console.log(line);
  });
like image 56
Quentin Avatar answered Sep 20 '22 19:09

Quentin