Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I spawn two processes from Node.js and pipe them together?

Tags:

node.js

spawn

I want to be able to spawn two Node.js child processes but have the stdout from one be piped to the stdin of another. The equivalent of:

curl "https://someurl.com" | jq .

My Node's stdout will go to either a terminal or to a file, depending on whether the user pipes the output or not.

like image 647
Glynn Bird Avatar asked Mar 19 '26 23:03

Glynn Bird


1 Answers

You can spawn a child process with Node.js's child_process built-in module. We need to processes, so we'll call it twice:

const cp = require('child_process')
const curl = cp.spawn('curl', ['https://someurl.com'], { stdio: ['inherit', 'pipe', 'inherit'] })
const jq = cp.spawn('jq', ['.'], { stdio: ['pipe', 'inherit', 'pipe'] })

The first parameter is the executable to run, the second is the array of parameters to pass it and the third is options. We need to tell it where the process's stdin, stdout and stderr are to be routed: 'inherit' means "use the host Node.js application's stdio", and 'pipe' means "we'll handle it programmatically.

So in this case curl's output and jq's input are left to be dealt with programmatically which we do with an additional line of code:

curl.stdout.pipe(jq.stdin)

which means "plumb curl's stdout into jq's stdin".

It's as simple as that.

like image 80
Glynn Bird Avatar answered Mar 22 '26 15:03

Glynn Bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!