TypeError: Cannot read properties of undefined (reading 'body') · Issue #1845 · node-fetch/node-fetch · GitHub
Skip to content

TypeError: Cannot read properties of undefined (reading 'body') #1845

Description

@vanaigr
.../repro/node_modules/node-fetch/src/index.js:216
                                                if (response_.statusCode !== 303 && request.body && options_.body instanceof Stream.Readable) {
                                                                                                             ^

TypeError: Cannot read properties of undefined (reading 'body')
    at ClientRequest.<anonymous> (.../repro/node_modules/node-fetch/src/index.js:216:68)
    at ClientRequest.emit (node:events:530:35)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:716:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
    at Socket.socketOnData (node:_http_client:558:22)
    at Socket.emit (node:events:530:35)
    at addChunk (node:internal/streams/readable:561:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
    at Readable.push (node:internal/streams/readable:392:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:189:23)

Node.js v22.14.0

Reproduction

minimal.mjs
import fetch from 'node-fetch'
import express from 'express'

const app = express()
app.post('/', (q, s) => {
    s.status(308)
    s.setHeader('location', '/2')
    s.send('')
})
app.post('/2', (q, s) => {
    s.status(302)
    s.setHeader('location', '/3')
    s.send('')
})

app.listen(54321, () => {
    ;(async() => {
        await fetch('http://localhost:54321', {
            method: 'POST',
            body: 'a',
        })
        console.log('Done')
    })()
})
package.json
{
  "dependencies": {
    "express": "^5.1.0",
    "node-fetch": "^3.3.2"
  }
}
  1. Place both files in a directory
  2. npm i
  3. node minimal.mjs
  4. Observe that the program crashes

Expected behavior

Program should not crash

Screenshots

N/A

Your Environment

Both linux and windows.
Node.js v22.14.0

Additional context

N/A

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions