#856·supertest

ERR_STREAM_WRITE_AFTER_END when piping request body

Author: alxndrsnCreated Apr 1, 2025Updated Jul 24, 2025
Labelsbug

Describe the bug

Node.js version: v22.14.0

supertest version: 7.1.0

Description: in some circumstances, calling stream.pipe(supertestRequest) can throw ERR_STREAM_WRITE_AFTER_END

Actual behavior

stream.pipe(supertestRequest) throws ERR_STREAM_WRITE_AFTER_END

Expected behavior

Should pipe the stream to the request.

Code to reproduce

The following requests with different stream types have different results with https://httpbin.org/post.

Changing the server name (e.g. https://httpbingo.org) can cause both types of streams to fail with ERR_STREAM_WRITE_AFTER_END, so server behaviour may be a factor.

working:

javascript
> req = require('supertest')('https://httpbin.org').post('/post'); stream.Readable.from('hi').pipe(req, { end: false }); try { await req; console.log({ status:rez.status, headers:rez.headers, body:rez.body }) } catch(err) { console.log('Caught:', err) }
{
  status: 200,
...

broken:

javascript
> req = require('supertest')('https://httpbin.org').post('/post'); rs = fs.createReadStream('./package.json'); rs.pipe(req, { end: false }); try { await req; console.log({ status:rez.status, headers:rez.headers, body:rez.body }) } catch(err) { console.log('Caught:', err) }
Uncaught Error [ERR_STREAM_WRITE_AFTER_END]: write after end

Reference:

javascript
> await fetch('https://httpbin.org/post', { method:'POST', body:stream.Readable.from('hi'), duplex:'half' })
Response {
  status: 200,
...
javascript
> await fetch('https://httpbin.org/post', { method:'POST', body:fs.createReadStream('./package.json'), duplex:'half' })
Response {
  status: 200,
...

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.