You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Node.js readline实现提问后换行输入响应?

解决方法

只需要在提问的提示文本末尾加上换行符\n,就能让光标跳到下一行等待输入。同时记得调用rl.close()关闭readline接口,避免程序卡住无法退出。

修改后的代码:

async function IsAgreed() {
    const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
    let options = ['yes', 'y', 'YES', 'Y'];
    // 提示语末尾添加\n实现换行
    let response = await rl.question('Do you agree to execute ?\n');
    // 关闭接口,防止程序无法退出
    rl.close();
    return options.includes(response);
}

修改后运行效果:

Do you agree to execute ?
| <-- 光标会在这里等待输入

内容的提问来源于stack exchange,提问作者user25384569

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 13:43:10