I see how to pipe stuff together using Node.js streams, but how do you pipe multiple scripts together using the Unix |
, given that some of these scripts can be async?
$ ./a.js | ./b.js
Example:
a.js (chmod 0755)
#!/usr/bin/env node
setTimeout(function(){
console.log(JSON.stringify({ foo: 'bar' }));
}, 10);
b.js (chmod 0755)
#!/usr/bin/env node
console.log(process.argv);
This is the output:
$ ./a.js | ./b.js
[ 'node', '/Users/viatropos/tests/b.js' ]
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:883:11)
at Object.afterWrite (net.js:700:19)
At first glance it seems like there's a lot going wrong, so not really sure where to start. Is there a way to get this to work? The end goal is to be able to take the console.log
output from ./a.js
and use it in ./b.js
. The reason is, most of the time these scripts will be run one at a time, but sometimes it would be nice to be able to pipe them together, so ideally the system should be able to handle both cases.