Skip to content
jshacks.dev
Async

Fix Error Cannot Pipe Not Readable in Node.js Streams

Fix the Node.js stream error cannot pipe, not readable, raised when pipe is called on a write-only stream, by correcting the order and using pipeline.

7 min read
Summarize in AI
Flat illustration of a log with a failing line and a wrench, with the Node.js, JavaScript, TypeScript, npm and Vite logos.

Error [ERR_STREAM_CANNOT_PIPE]: Cannot pipe, not readable is Node.js telling you that you called .pipe() on a stream that can only be written to. A Writable has no data to send anywhere, so asking it to pipe makes no sense, and the runtime reports that with this error. The mistake is almost always one of three things: the two ends of the pipe are the wrong way round, a function returned a write stream where you expected a read stream, or an object such as an HTTP response was treated as a source when it is a destination. This post reproduces the error, shows how to tell the stream kinds apart, and fixes each cause, finishing with pipeline, which catches the mistake in a clearer way. The examples run on Node.js 24.

Reproducing it

The method pipe() belongs to readable streams. On a write-only stream, Node.js defines pipe() only to report the mistake.

reproduce.mjsJavaScript
import { Writable } from "node:stream";

const noop = () =>
  new Writable({
    write(chunk, encoding, callback) {
      callback();
    },
  });

const destination = noop();
destination.on("error", (error) => {
  console.log(error.code);
  console.log(error.message);
});

destination.pipe(noop());
Text
ERR_STREAM_CANNOT_PIPE
Cannot pipe, not readable

The error is delivered as an error event on the stream you called pipe on, here destination. If nothing listens for error on that stream, Node.js throws it as an uncaught exception and the process exits, which is why this problem often shows up as a crash rather than a handled failure. The code property is the stable thing to test, and the message is for people.

How to tell what a stream is

Streams come in four kinds. A Readable produces data and has pipe() and read(). A Writable consumes data and has write() and end(). A Duplex is both, such as a TCP socket. A Transform is a duplex whose output is computed from its input. The properties readable and writable tell you which a given object supports.

kinds.mjsJavaScript
import { Readable, Writable, Transform } from "node:stream";

const kinds = {
  Readable: Readable.from(["x"]),
  Writable: new Writable({ write: (c, e, cb) => cb() }),
  Transform: new Transform({ transform: (c, e, cb) => cb(null, c) }),
};

for (const [name, stream] of Object.entries(kinds)) {
  console.log(name.padEnd(9), "readable:", stream.readable, "writable:", stream.writable);
}
Text
Readable  readable: true writable: false
Writable  readable: false writable: true
Transform readable: true writable: true

A stream with readable: false cannot be the left-hand side of a pipe. Printing these two flags on the object in question is the quickest diagnosis, and it turns "which stream is the problem" into a one-line check.

Cause 1: the pipe is written backwards

Data flows from left to right: source.pipe(destination). Reading the line aloud as "source pipes into destination" catches the reversal.

backwards.mjsJavaScript
import { createReadStream, createWriteStream } from "node:fs";
import { writeFile, readFile, rm } from "node:fs/promises";

await writeFile("in.txt", "copy me");

const input = createReadStream("in.txt");
const output = createWriteStream("out.txt");

output.on("error", (error) => console.log("wrong order:", error.code));
output.pipe(input); // backwards

await new Promise((resolve) => setTimeout(resolve, 20));

createReadStream("in.txt").pipe(createWriteStream("out.txt")).on("finish", async () => {
  console.log("right order:", await readFile("out.txt", "utf8"));
  await rm("in.txt");
  await rm("out.txt");
});
Text
wrong order: ERR_STREAM_CANNOT_PIPE
right order: copy me

The reversed call fails, and the corrected line copies the file. The usual way this happens is a refactor that moves variables around, or a chain of several pipes where one link is flipped. In a chain a.pipe(b).pipe(c), pipe() returns its destination, so each stage after the first must be a duplex or transform to continue the chain, and a plain Writable can only be the last link. Adding another .pipe() after a Writable produces this same error, because the thing you are now piping from is write-only.

chain-end.mjsJavaScript
import { Readable, Writable } from "node:stream";

const sink = new Writable({ write: (c, e, cb) => cb() });
sink.on("error", (error) => console.log(error.code));

Readable.from(["a"]).pipe(sink).pipe(new Writable({ write: (c, e, cb) => cb() }));
Text
ERR_STREAM_CANNOT_PIPE

pipe(sink) returns sink, and the second .pipe() is then called on a write-only stream. If you want the data to go to two places, pipe the source twice, not the sink onwards.

Cause 2: a function returns the wrong kind of stream

A helper named openFile might return a write stream in one place and a read stream in another, and the caller assumes a read stream. Naming helps: openReader and openWriter leave no doubt. Type definitions help more. In TypeScript, a function that declares Readable as its return type will not compile if it returns a WriteStream, and a caller cannot pipe from a Writable without a type error, since the Writable type has no pipe method.

Cause 3: treating an HTTP response as a source

In a Node.js HTTP server, req is a readable stream of the request body and res is a writable stream for the response. Piping from res is the typical error here.

response.mjsJavaScript
import { createServer } from "node:http";
import { Readable } from "node:stream";

createServer((req, res) => {
  // Wrong: res is write-only, so res.pipe(...) raises ERR_STREAM_CANNOT_PIPE
  // res.pipe(process.stdout);

  // Right: pipe a readable into res
  Readable.from(["ok"]).pipe(res);
}).listen(3000);

The commented line, if enabled, raises the error on the response, and the active line delivers the body. The client side differs: with http.request, the object you get in the callback, here response, is a readable IncomingMessage and can be piped from, while the request object itself is writable. For fetch, response.body is a web ReadableStream without .pipe(), which fails with a plain TypeError instead, and you use Readable.fromWeb(response.body) or pipeline, as described in Node streams vs web streams.

The robust fix: use pipeline

pipeline takes the streams in the order the data flows and reports any failure through one promise, with every stage cleaned up on error.

use-pipeline.mjsJavaScript
import { Readable, Writable } from "node:stream";
import { pipeline } from "node:stream/promises";

const sink = new Writable({ write: (chunk, encoding, callback) => callback() });

await pipeline(Readable.from(["a", "b"]), sink);
console.log("pipeline finished");
Text
pipeline finished

The first argument of pipeline is documented as the source, so the call reads in the same order as the data flows, and any failure in any stage rejects the returned promise. You handle it with an ordinary try/catch instead of an error event on each stream, and every stream in the chain is destroyed for you. A pipeline built from a mistaken stage fails as a rejection you can log, instead of an uncaught error event, which is the practical gain. Replacing manual pipe() chains with pipeline removes this whole class of crash.

Add an error handler or use pipeline

Whatever fix you choose, remember that streams emit errors as events. A stream with no error listener turns any error into an uncaught exception. When you use pipe(), attach error listeners to every stream in the chain, since pipe() does not forward errors from one stream to the next. With pipeline you get one place to catch them all, which is the second reason to prefer it.

Where the advice stops

Do not "fix" the error by making the destination readable. If you genuinely need to read what you wrote, use a Transform or PassThrough, which is both. And when a third-party library throws this error from inside, the library is piping the wrong stream and the bug is in its source, or in the arguments you gave it, such as swapping an input and an output option.

Finding the offending line in a large codebase

When the error comes from a long file or a library wrapper, the stack trace names the place where the error event was raised, which is the pipe call or a frame right below it. Start there. Read the line from left to right and ask, for each object, whether it can produce data. If a value comes from a function, open the function and check which kind of stream it returns. Names help here: a helper called createOutput that returns a WriteStream is fine, while a variable called stream that is sometimes a reader and sometimes a writer is a bug waiting for a reversal.

Search the project for .pipe( and read each one, because the same mistake tends to repeat wherever a pattern was copied. A pass of five minutes with a list of the calls often finds two or three more reversed pipes that simply have not run yet, for example on an error path that is rarely taken. Where a call can be replaced with pipeline, do it while you are there, since the replacement is mechanical and the error handling is better.

Also inNode.js
Taggederror fix
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.