oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one

Home Page:https://bun.sh

Repository from Github https://github.comoven-sh/bunRepository from Github https://github.comoven-sh/bun

confusing error: "TypeError: Expected fetch() to be a function" in Bun.serve

johnfn opened this issue · comments

What version of Bun is running?

1.2.5+013fdddc6

What platform is your computer?

Darwin 24.0.0 arm64 arm

What steps can reproduce the bug?

The error seems to be exactly the same as the one in this closed issue #4555, though the way to reproduce it is more convoluted.

In particular, bun crashes on Bun.serve(...) with the error TypeError: Expected fetch() to be a function.

I'm able to reproduce this error in SST (which runs bun), but it doesn't reproduce in normal bun. I suspect something about the SST environment is causing this error, though I'm not sure what. In any case, I'm pretty sure bun should never produce this error.

My bun server:

import index from "./client/index.html";

const port = 3000;
const server = Bun.serve({
  tls: {
    key: await Bun.file("certs/localhost-key.pem").text(),
    cert: await Bun.file("certs/localhost.pem").text(),
  },
  // routes: wrapRoutes(ApiRoutes),
  routes: {
    "/": index,
  },
  port,
});

console.log(`Server is running on port ${port}`);

My SST config:

/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
  app(input) {
    return {
      name: "pandan",
      removal: input?.stage === "prod" ? "retain" : "remove",
      protect: ["prod"].includes(input?.stage),
      home: "aws",
      providers: {
        aws: {
          profile: input.stage === "prod" ? "pandan-prod" : "pandan-dev",
        },
      },
    };
  },
  async run() {
    const vpc = new sst.aws.Vpc("MyVpc");

    const cluster = new sst.aws.Cluster("MyCluster", { vpc });

    new sst.aws.Service("PandanWeb", {
      cluster,
      loadBalancer: {
        ports: [{ listen: "80/http", forward: "3000/http" }],
      },
      dev: {
        command: "bun dev",
      },
      link: [auth],
    });
  },
});

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@thdxr is SST using bun build --target=bun ./index.js?