maxedison
maxedison

Reputation: 17563

Streaming response from child_process.spawn curl request

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.

Upvotes: 1

Views: 3283

Answers (1)

robertklep
robertklep

Reputation: 203359

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' ])

Upvotes: 9

Related Questions