- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想使用自定义流来处理 child_process.spawn stdio。
例如
const cp = require('child_process');
const process = require('process');
const stream = require('stream');
var customStream = new stream.Stream();
customStream.on('data', function (chunk) {
console.log(chunk);
});
cp.spawn('ls', [], {
stdio: [null, customStream, process.stderr]
});
我收到错误 stdio 流的值不正确
。
有 child_process.spawn https://nodejs.org/api/child_process.html#child_process_options_stdio 的文档.它表示 stdio 选项可以采用 Stream 对象
Stream object - Share a readable or writable stream that refers to a tty, file, socket, or a pipe with the child process.
我想我错过了这个“引用”部分。
最佳答案
这似乎是一个错误:https://github.com/nodejs/node-v0.x-archive/issues/4030 customStream
在传递给 spawn() 时似乎还没有准备好。您可以轻松解决此问题:
const cp = require('child_process');
const stream = require('stream');
// use a Writable stream
var customStream = new stream.Writable();
customStream._write = function (data) {
console.log(data.toString());
};
// 'pipe' option will keep the original cp.stdout
// 'inherit' will use the parent process stdio
var child = cp.spawn('ls', [], {
stdio: [null, 'pipe', 'inherit']
});
// pipe to your stream
child.stdout.pipe(customStream);
关于node.js - Nodejs child_process 生成自定义 stdio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34967278/
.bat文件为:。在命令提示符下运行cmd/c a.bat将打开记事本窗口。并且在关闭窗口之前打印Foobar。我可以在不关闭记事本的情况下运行下一个命令。。但是,在node.js child_pro
有一段时间以来,我一直在尝试达成一些目前对我来说还行不通的事情。 对于 nodejs,我喜欢运行交互式 sh-command 并在命令退出后使用 sh-command 输出。我喜欢编写一个可生成的生成
我正在使用 Node.js 下的 Bluebird Promise 库,非常棒!但我有一个问题: 如果您查看 Node 的文档 child_process.exec和 child_process.ex
此代码适用于 Windows 和 Mac OS X: var exec = require( 'child_process' ).exec exec( 'git clone git@github.co
我在MacOS和Linux中使用node.js v0.10.12时遇到了这个错误,这段代码在 Windows 7 中工作正常。唯一的区别是在 Windows 7 上我有管理员 ID,在 MacOSX
我在 Electron 项目中使用 npm 库“child_process”来捕获带有 spawn 的 C 程序的标准输出流。但是,当我尝试记录 child.stdout.on('data') 捕获的
在 atom-text-editor 插件开发中,我运行一个 child_process,如 cp = require 'child_process' command = "python /home/
我正在尝试在我的 Node 脚本中创建一个虚拟 bash,它从另一个用户上下文中执行命令。 var spawn = require('child_process').spawn; var termin
我有一个在 electron 中运行的 angular2 webpack 项目。我正在尝试使用 shelljs 但 webpack 无法构建并出现错误: [0] ERROR in ./~/shellj
React Native v0.23.0 Node v5.11.0 Windows 10 10586(我在 OSX 上尝试并得到相同的错误) 我尝试在React Native中编写var cp = r
try { var output = child_process.execSync(cmd); } catch (ex) { return 0 } return output.toSt
我正在尝试使用 node.js 执行一堆 bash 命令。我使用 child_process.exec 来执行命令。 var child_process = require('child_proces
这个问题在这里已经有了答案: How to print both to stdout and file in C (4 个答案) Writing to both stdout & a file (7
我正在使用 child_process 运行最终调用的脚本 ssh -t server "sudo command" 我回来了: Pseudo-terminal will not be allocat
我正在尝试在 Node 中执行一个子进程,其名称来自发布请求。我使用的是express,因此是app.post()。我的代码如下。 app.post('/*', function(req, res)
我需要验证 child_process 是否已成功终止,因为如果该进程仍然存在,我将无法执行下一步操作。 var proc = require('child_process'); var prog =
我正在尝试使用目标设备启动 cordova 命令。我已经测试了该命令并且它有效,但是当我尝试用我的代码生成它时,它忽略等号,因此不会运行。此代码确实可以工作,只是不添加 "--target='iPho
我正在使用 exec() 运行终端命令以在 electron mac 应用程序中显示对话框。 我使用的代码: var exec = require('child_process').exec; var
如何在 node.js 上生成此命令 (/usr/bin/which flac): var spawn = require('child_process').spawn; var cmd = spaw
我正在尝试 execute a child在不同的目录中处理,然后是其父目录。 var exec = require('child_process').exec; exec( 'pwd',
我是一名优秀的程序员,十分优秀!