Search code examples
streamfetchsveltekit

SvelteKit Node streams are no longer supported


Since node-fetch was replaced by undici in #5117 some of us encountered the error

Node streams are no longer supported — use a ReadableStream instead

like in this post

It is not easy to reproduce, for me the error occured only in production.

This is a self-answered question in case you have the same problem.


Solution

  • The error comes from src/runtime/server/utils.js L46 and is thrown after checking the _readableState property and some type on the response body of the request.

    For me the problem was that my endpoint.ts was returning the fetch directly.

    export async function post({request}){
      return fetch('...')
    }
    
    

    This used to work but not anymore since the fetch response is a complex object with the _readableState property. To fix this you have to consume the response and return a simpler object like

    export async function post({request}){
      try {
        const res = await fetch('...')
        const data = await res.json()
        return {
          status: 200,
          body: JSON.stringify({...data}),
        }
      catch(error){
        return { status: 500}
      }
    }