Streaming response from child_process.spawn curl request

随声附和 提交于 2019-12-11 04:15:32

问题


I'm trying to run the cURL command to install RVM and ruby via child_process.spawn, but it always errors out:

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)
})

The error I receive is:

{"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)

The JSON-prettified version, without the stacktrace, is:

{
    "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": []
}

It works fine if I use child_process.exec, but I'd like to be able to stream the output.


回答1:


child_process.spawn() should get passed the name of a command to run, and a list of it's arguments. You're feeding it a shell pipeline.

For that to work, you need to run a shell and pass the pipeline as an argument:

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


来源:https://stackoverflow.com/questions/37229144/streaming-response-from-child-process-spawn-curl-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!