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
- Import fs:
const fs = require('fs');. - Create a read stream:
const readStream = fs.createReadStream('input.txt', { encoding: 'utf8' });. - Create a write stream:
const writeStream = fs.createWriteStream('output.txt');. - Listen for data on the read stream:
readStream.on('data', chunk => writeStream.write(chunk));. - Listen for the end event:
readStream.on('end', () => writeStream.end());.
Tips
- Use the
highWaterMarkoption 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
errorevents on streams can crash the process with uncaught exceptions. - Writing faster than the consumer can read causes backpressure; use
.pipe()which handles this automatically.