Home > Enterprise >  TypeError: readline.createInterface is not a function
TypeError: readline.createInterface is not a function

Time:04-11

I am trying to read input from console using Node.

I installed readline-promise:

npm install readline-promise

and it is correctly installed in node_modules.

Code:

import readline from "readline-promise";

const rlp = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: true,
});

And I get the following error:

TypeError: readline.createInterface is not a function

CodePudding user response:

For reasons I don't understand, the readline-promise module does not seem to work the way its documentation says it should when importing from an ESM module using import.

The default export is not working properly the way the doc shows so it appears you have to manually get the right entry points.

After examining the object you get when you import readline-promise, this is what I was able to get to work:

import rl from "readline-promise";
const readline = rl.default;

const rlp = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: true,
});
  • Related