Interview Q&A / Node.js

IntermediateQuick answer1 min readUpdated 3 Oct 2026

What is the difference between stream.pipe and stream.pipeline?

SHORT ANSWER

pipe connects streams but does not forward errors or clean up, so a failure can leave streams open. pipeline connects several streams, destroys all of them on error, and reports the result in one callback or promise.

Explain it simply

Imagine joining three pipes with plain tape. If the middle pipe bursts, water leaks and the other two keep running. pipeline is like fitting the pipes with a master switch: if any part fails, everything shuts down and you are told once.

Example

js
const fs = require("fs");
const zlib = require("zlib");
const { pipeline } = require("stream/promises");

// pipe: you must listen for errors on every stream yourself
fs.createReadStream("in.txt")
  .pipe(zlib.createGzip())
  .pipe(fs.createWriteStream("out.gz"));

// pipeline: one place for errors, automatic cleanup
async function compress() {
  try {
    await pipeline(
      fs.createReadStream("in.txt"),
      zlib.createGzip(),
      fs.createWriteStream("out.gz")
    );
    console.log("done");
  } catch (err) {
    console.error("failed:", err.message);
  }
}
compress();

With plain pipe, if the source errors, the destination is not closed, which can leak file descriptors. An error on the destination does not stop the source either.

Since Node 15 there is a promise version in stream/promises, which fits naturally with await.

What to say in the interview

"pipe does flow control but no error propagation or cleanup. pipeline handles both, destroying every stream on failure and returning a promise or callback, so I use pipeline in production code."

Common mistakes

  • Attaching an error handler only to the last stream in a pipe chain.
  • Forgetting that an unhandled error event crashes the process.

#nodejs#streams#error-handling

Related questions

Practise more Node.js questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Node.js quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.