'TypeError: rl.question is not a function - NodeJS - using play.js
I’m trying to use the readline library within play.js which is an iOS app on my iPad.
const rl = require("readline")
rl.createInterface({
input: process.stdin,
output: process.stdout
});
var response
rl.question("Enter a number ", function (answer) {
response = answer
outside();
rl.close();
});
outside = function(){
console.log('The user entered: ', response)
}
And I keep getting the error
TypeError: rl.question is not a function
Solution 1:[1]
Apparently the readline
npm package has been renamed to linebyline
because it is now the name of a core module in node 10. I would suggest changing your import like this:
const rl = require("linebyline");
If this does not solve the problem, please check if you installed the correct package. The error seems to indicate that rl.question
is undefined
.
Edit: My bad, I see now you intended to use the core module. Then maybe check if your node setup works correctly and if the version is recent enough. (Also try adding a ;
after var response
.)
Solution 2:[2]
Ok I know that I'm three months late but this is my first time replying with an answer that works!!! But anyway the reason that you keep getting an error is because this line
const rl = require("readline")
only pulls the readline module into rl, if you wanted to do anything with readline you need to create an instance of it by doing.
const rlInstance = rl.createInterface({
input: process.stdin,
output: process.stdout
});
and then call the question method from that object like this
rlInstance.question("Enter a number ", function(answer) {
response = answer
outside();
rlInstance.close();
});
I'm still learning JS so I could be wrong in my reasoning but the code will work nonetheless :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |