node-fetch/node-fetch · error · FetchError
system
system
Error message
Could not create Buffer from response body for ${data.url}: ${error.message} What it means
A FetchError with code 'system' thrown at src/body.js:246 when Buffer.concat (or Buffer.from on string chunks) fails while materializing the accumulated response bytes. node-fetch gathers stream chunks in an array and concatenates them at the end; if those chunks are not valid Buffer-compatible values the concat throws and is rewrapped as a FetchError so callers can distinguish transport-level corruption from application errors.
Source
Thrown at src/body.js:246
}
accumBytes += chunk.length;
accum.push(chunk);
}
} catch (error) {
const error_ = error instanceof FetchBaseError ? error : new FetchError(`Invalid response body while trying to fetch ${data.url}: ${error.message}`, 'system', error);
throw error_;
}
if (body.readableEnded === true || body._readableState.ended === true) {
try {
if (accum.every(c => typeof c === 'string')) {
return Buffer.from(accum.join(''));
}
return Buffer.concat(accum, accumBytes);
} catch (error) {
throw new FetchError(`Could not create Buffer from response body for ${data.url}: ${error.message}`, 'system', error);
}
} else {
throw new FetchError(`Premature close of server response while trying to fetch ${data.url}`);
}
}
/**
* Clone body given Res/Req instance
*
* @param Mixed instance Response or Request instance
* @param String highWaterMark highWaterMark for both PassThrough body streams
* @return Mixed
*/
export const clone = (instance, highWaterMark) => {
let p1;
let p2;
let {body} = instance[INTERNALS];
View on GitHub (pinned to 8b3320d2a7)
Solutions
- Inspect the underlying cause on error.cause — it carries the original message
- Verify the upstream server is sending well-formed bytes (curl the endpoint and compare)
- If you control the source stream, ensure it pushes Buffers or Uint8Arrays only
- For very large responses, stream instead of buffering (consume response.body directly)
Example fix
// before
const buf = await response.buffer();
// after - stream the body instead of buffering it whole
for await (const chunk of response.body) {
process.stdout.write(chunk);
} Defensive patterns
Strategy: try-catch
Validate before calling
// Limit response size before buffering to avoid memory issues
const MAX = 50 * 1024 * 1024; // 50MB ceiling
if (response.headers.get('content-length') && Number(response.headers.get('content-length')) > MAX) {
throw new Error('Response too large to buffer; stream it instead');
} Try / catch
try {
const buf = await response.arrayBuffer();
} catch (e) {
if (e?.code === 'system' && /Could not create Buffer/.test(e.message)) {
// upstream is emitting bad chunks - re-fetch or switch to streaming
}
throw e;
} Prevention
- Stream response.body directly for large payloads instead of buffering
- Validate the upstream server emits Buffers/Uint8Arrays
- Set a sensible response size cap in your fetch wrapper
- Log e.cause to capture the original Buffer.concat error
When it happens
Trigger: A response stream emitting chunks that are not Buffers/Uint8Arrays (custom push of strings or objects on a hand-rolled server); memory exhaustion where Buffer.concat cannot allocate; a corrupted stream where the upstream pushes unexpected chunk types after a transform.
Common situations: Proxies or test servers that push non-Buffer data into the readable; very large responses hitting process memory limits; interop with streams that default to objectMode; broken compression pipelines feeding garbage chunks.
Related errors
- body used already for: ${data.url}
- cannot clone body after it is used
- Premature close of server response while trying to fetch ${d
- Request with GET/HEAD method cannot have body
- MultipartParser.end(): stream ended unexpectedly
AI-assisted analysis of node-fetch/node-fetch@8b3320d2a7 (2026-08-03).
Data as JSON: /data/errors/c6dab36003e8480d.json.
Report an issue: GitHub.