Node readline module doesn't have 'on' function?
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);
});
You are trying to call the method on the module, not on the result of createInterface()
Instead of this:
var lineReader = require('readline');
lineReader.createInterface({
input: fs.createReadStream('./testfile')
});
lineReader.on('line', function(line){
console.log(line);
});
try this:
var readline = require('readline');
var lineReader = readline.createInterface({
input: fs.createReadStream('./testfile')
});
lineReader.on('line', function(line){
console.log(line);
});
See the docs at http://node.readthedocs.io/en/latest/api/readline/
Example:
var readline = require('readline'),
rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('OHAI> ');
rl.prompt();
rl.on('line', function(line) {
switch(line.trim()) {
case 'hello':
console.log('world!');
break;
default:
console.log('Say what? I might have heard `' + line.trim() + '`');
break;
}
rl.prompt();
}).on('close', function() {
console.log('Have a great day!');
process.exit(0);
});
As you can see the .on()
is called on the result of calling .createInterface()
- not on the same object that the .createInterface()
method was called on.