TypeError: readline.createInterface is not a function

TypeError: readline.createInterface is not a function

我正在尝试使用 Node 从控制台读取输入。

我安装了 readline-promise:

npm install readline-promise

并且已正确安装在 node_modules。

代码:

import readline from "readline-promise";

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

我收到以下错误:

TypeError: readline.createInterface is not a function

由于我不明白的原因,readline-promise 模块在使用 import 从 ESM 模块导入时似乎不像其 documentation 所说的那样工作。

默认导出无法按照文档显示的方式正常工作,因此您必须手动获取正确的入口点。

检查导入时获得的对象后 readline-promise,这就是我能够开始工作的内容:

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

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