【发布时间】:2020-09-24 16:56:59
【问题描述】:
我有一个非常简单的 Node.js 程序,它使用readline-sync 接受输入,然后将其回显到控制台:
const readlineSync = require('readline-sync');
const input = readlineSync.prompt();
console.log(input);
它作为一个交互式程序运行良好;但是,当我尝试将输入通过管道传递给它时(在 Git Bash 或 PowerShell 中),我得到一个 Node.js 错误:
PS> echo "1.2" | node .\index.js
Windows PowerShell[35552]: c:\ws\src\node_file.cc:1631: Assertion `(argc) == (5)' failed.
添加#!/usr/bin/env node shebang 并将其作为带有echo "1.2" | .\script.js 的脚本运行会产生相同的错误。
是否有允许readline-sync 从管道读取输入的配置选项或我缺少的东西?我在 shell 中运行它的方式有问题吗?任何建议将不胜感激。
【问题讨论】: