How to stream large files in Node.js

· Category: Node.js

Short answer

Use Node.js streams to process data in chunks rather than loading entire files into memory. Pipe a fs.createReadStream() to a fs.createWriteStream() or an HTTP response for efficient transfer.

Details

Node.js provides four stream types: Readable, Writable, Duplex, and Transform. For file operations, createReadStream emits data events as chunks arrive. You can pipe directly to a response object to support HTTP range requests and backpressure handling automatically.

Streaming is essential for video delivery, CSV parsing, and log processing. If you are handling file uploads in a web app, you may also want to read How to handle file uploads with Multer in Express. For transforming data on the fly, how to use array map filter reduce offers conceptual parallels, though stream transforms use a different API.

Tips

  • Always handle error events on streams; uncaught stream errors crash the process.
  • Use pipeline() from the stream module instead of multiple .pipe() calls for automatic cleanup on errors.
  • For production logging of streamed data, see How to set up Python logging if you have hybrid services.