How to stream to/from a file descriptor in node?

Bartvds picture Bartvds · Jul 5, 2014 · Viewed 8.7k times · Source

The fs.createReadStream() and fs.createWriteStream() only support file paths but I need to read (or write) from a file descriptor (passed to/from a child process).

Note I need Streams, so fs.open/fs.read/fs.write are not sufficient.

Answer

Joe Hildebrand picture Joe Hildebrand · Jul 5, 2014

When you call fs.createReadStream you can pass in a file descriptor:

var fs = require('fs');
var fd = fs.openSync('/tmp/tmp.js', 'r');
var s = fs.createReadStream(null, {fd: fd});
s.pipe(process.stdout);

If there is a fd option, the filename is ignored.