来自 child_process.spawn curl 请求的流式响应

Streaming response from child_process.spawn curl request

我正在尝试 运行 cURL 命令通过 child_process.spawn 安装 RVM 和 ruby,但它总是出错:

let spawnProcess = spawn('\curl -sSL https://get.rvm.io | bash -s stable --ruby')

spawnProcess.stdout.on('data', data => {
    console.log('DATA RECEIVED')
    console.log(data)
})

spawnProcess.stdout.on('close', () => {
    alert('done!')
})

spawnProcess.stderr.on('data', function(){
    console.log('ON DATA')
    console.log(arguments)
})

spawnProcess.on('error', error => {
    console.log('ON ERROR')
    console.log(JSON.stringify(error), error)
})

我收到的错误是:

{"code":"ENOENT","errno":"ENOENT","syscall":"spawn curl -sSL https://get.rvm.io | bash -s stable --ruby","path":"curl -sSL https://get.rvm.io | bash -s stable --ruby","spawnargs":[]} Error: spawn curl -sSL https://get.rvm.io | bash -s stable --ruby ENOENT
    at exports._errnoException (util.js:890:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:182:32)
    at onErrorNT (internal/child_process.js:348:16)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

没有堆栈跟踪的 JSON 美化版本是:

{
    "code": "ENOENT",
    "errno": "ENOENT",
    "syscall": "spawn curl -sSL https://get.rvm.io | bash -s stable --ruby",
    "path": "curl -sSL https://get.rvm.io | bash -s stable --ruby",
    "spawnargs": []
}

如果我使用 child_process.exec,它工作正常,但我希望能够流式传输输出。

child_process.spawn() 应该将命令的名称传递给 运行,以及它的参数列表。您正在为其提供 shell 管道。

为此,您需要 运行 一个 shell 并将管道作为参数传递:

let spawnProcess = spawn('/bin/sh', [ '-c', 'curl -sSL https://get.rvm.io | bash -s stable --ruby' ])