Piping Streams

pipe connects a readable stream directly to a writable one.

Syntaxreadable.pipe(writable);

The pipe() method connects a readable stream to a writable stream, automatically moving chunks from one to the other. It is the cleanest way to copy or transform data.

A readable stream piping data chunks into a writable streamReadablesource file.pipe()Writabledestinationchunks flow one at a time
pipe pushes chunks from a readable source to a writable destination.

pipe also handles backpressure — it pauses the source if the destination cannot keep up.

Example

Example · javascript
const fs = require('fs');

// Copy a file by piping read -> write
const src = fs.createReadStream('input.txt');
const dest = fs.createWriteStream('copy.txt');

src.pipe(dest);
dest.on('finish', () => console.log('File copied!'));

When to use it

  • A file-copy utility pipes a readable file stream directly into a writable file stream, transferring gigabytes without ever buffering the whole file.
  • A compression service pipes an HTTP request body through `zlib.createGzip()` and then into `fs.createWriteStream` to store compressed uploads.
  • A streaming JSON API pipes a readable stream through a stringify transform and directly into the HTTP response to start sending data before it is fully assembled.

More examples

Pipe file copy

Copies a file by piping the readable stream into a writable stream -- handles backpressure automatically.

Example · js
const fs = require('fs');

fs.createReadStream('input.txt')
  .pipe(fs.createWriteStream('output.txt'))
  .on('finish', () => console.log('Copy complete'));

Pipe through gzip compression

Chains two `.pipe()` calls to compress a file with gzip inline -- no intermediate buffers in application code.

Example · js
const fs   = require('fs');
const zlib = require('zlib');

fs.createReadStream('data.json')
  .pipe(zlib.createGzip())
  .pipe(fs.createWriteStream('data.json.gz'))
  .on('finish', () => console.log('Compressed'));

stream.pipeline for error safety

Uses `stream/promises.pipeline` -- the modern, error-safe alternative to chaining `.pipe()` calls.

Example · js
const { pipeline } = require('stream/promises');
const fs   = require('fs');
const zlib = require('zlib');

await pipeline(
  fs.createReadStream('archive.tar'),
  zlib.createGunzip(),
  fs.createWriteStream('archive-extracted.tar')
);
console.log('Pipeline done');

Discussion

  • Be the first to comment on this lesson.