Node.js中使用readline-sync获取用户输入报错退出码134求助
解决Node.js中readline-sync报错Process exited with code 134的问题
可能的原因及解决方案
1. ES模块兼容性问题
readline-sync对ES模块(import语法)的支持存在兼容性问题,建议改用CommonJS规范测试:
const readlineSync = require('readline-sync'); const name = readlineSync.question("Whats the name of the movie?"); console.log(name);
如果坚持使用ES模块,确保package.json中设置了"type": "module",同时重新安装依赖:
npm uninstall readline-sync npm install readline-sync
2. VS Code终端/调试模式限制
VS Code的调试控制台不支持交互式输入,若通过调试模式运行脚本,会导致readline-sync无法获取输入从而报错。解决方法:
- 直接在VS Code的集成终端(Terminal面板)中运行脚本:
node your-file.js - 切换到系统原生终端(Windows CMD/PowerShell、macOS/Linux终端)执行脚本
3. 终端交互权限问题
错误码134通常关联SIGABRT信号,大概率是终端无法提供交互式输入环境。检查:
- 确保运行脚本时终端处于交互式模式,没有被重定向输入(比如通过管道或文件输入)
- 若在WSL环境下,尝试重启WSL或检查终端权限设置
4. 改用替代方案
如果上述方法无效,可以尝试以下替代方案:
方案一:使用prompt-sync稳定同步输入库
安装依赖:
npm install prompt-sync
代码示例:
const prompt = require('prompt-sync')(); const name = prompt("Whats the name of the movie?"); console.log(name);
方案二:封装原生readline为同步调用
原生readline为异步模块,可通过Promise封装成同步逻辑:
const readline = require('readline'); const { promisify } = require('util'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const question = promisify(rl.question).bind(rl); async function main() { const name = await question("Whats the name of the movie?"); console.log(name); rl.close(); } main();
内容的提问来源于stack exchange,提问作者Adrian
相关产品推荐
相关产品推荐

