How to create read and write streams

· Category: Node.js

Short answer

Use fs.createReadStream() to read files as streams and fs.createWriteStream() to write files as streams.

Steps

  1. Import fs: const fs = require('fs');.
  2. Create a read stream: const readStream = fs.createReadStream('input.txt', { encoding: 'utf8' });.
  3. Create a write stream: const writeStream = fs.createWriteStream('output.txt');.
  4. Listen for data on the read stream: readStream.on('data', chunk => writeStream.write(chunk));.
  5. Listen for the end event: readStream.on('end', () => writeStream.end());.

Tips

  • Use the highWaterMark option to control the chunk size and balance memory usage versus throughput.
  • Enable autoClose to ensure file descriptors are released after the stream finishes.

Common issues

  • Not handling error events on streams can crash the process with uncaught exceptions.
  • Writing faster than the consumer can read causes backpressure; use .pipe() which handles this automatically.