oven-sh / bun

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

Home Page:https://bun.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bun build fails: The superclass is not a constructor

laurensV opened this issue · comments

What version of Bun is running?

1.0.26+c75e768a6

What platform is your computer?

Linux 5.15.133.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Some packages run just fine when using bun directly, but when trying to use bun build it gives this error TypeError: The superclass is not a constructor.

I created a small example repo:
First install the package, for example @solana/web3.js

bun install @solana/web3.js

create file index.js that uses the package:

import * as web3 from '@solana/web3.js';
console.log(web3);

Running this file directly with bun index.js works fine, but when trying to build first and then run it (or when using --compile flag to put it into one executable), it fails:

bun build ./index.js --outdir ./build --target=bun && bun build/index.js
13821 | var fetchImpl = typeof globalThis.fetch === "function" ? globalThis.fetch : async function(input, init) {
13822 |   const processedInput = typeof input === "string" && input.slice(0, 2) === "//" ? "https:" + input : input;
13823 |   return await nodeFetch.default(processedInput, init);
13824 | };
13825 | 
13826 | class RpcWebSocketClient extends client.default {
        ^
TypeError: The superclass is not a constructor.
      at /$bunfs/root/test:13826:1

Seen this behaviour in multiple packages now, like puppeteer (#6261) and AWS CDK (#3088)

You can find the example repo code here: https://github.com/laurensV/bun-build-bug-solana-web3js

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

I got the same issue, with the puppeteer package.

Observing the same incorrect behavior when trying to build code importing the jspdf package.

Upon further testing, at least for the jspdf package, it appears the issue arises when build with bun build [...] --target=bun. When using bun build [...] --target=node, it works fine, even when the bundled code is executed with bun.

After more testing, the bundle built with --target=node starts without errors, but it still errors at runtime when constructing the same class that causes parsing issues on startup when bundled with --target=bun.

Same problem without our CLI tools here: https://github.com/sprucelabsai-community/spruce-cli-workspace

  1. cd packages/spruce-cli
  2. bun build ./src/index.ts --compile --outfile=spruce
  3. ./spruce

Experiencing same issue

❯ bun -v
1.1.10
bun build ./index.js --compile --outfile mycli
❯ ./mycli
45513 |     this._process();
45514 |     return this._hash;
45515 |   }
45516 | }
45517 |
45518 | class _Readable extends EventEmitter2 {
        ^
TypeError: The superclass is not a constructor.
      at /$bunfs/root/mycli:45518:1

I got the same issue when using jsx-email (shellscape/jsx-email#197)

Here is a minimal reproduction example https://github.com/nephix/jsx-email-repro (bun 1.1.12)

It works fine with

bun run index.ts

but fails with

bun run build
bun dist/index.js

│ 132468 |
│ 132469 | class Registry2 extends Registry$1 {
│          ^
│ TypeError: The superclass is not a constructor.
│       at /Users/nephix/Projects/boilerplate/apps/backend/dist/index.js:132469:1
│ note: missing sourcemaps for /Users/nephix/Projects/boilerplate/apps/backend/dist/index.js
│ note: consider bundling with '--sourcemap' to get an unminified traces
│
│ Bun v1.1.12 (macOS arm64)
│ error: script "start" exited with code 1

Encountered the same issue with W3CBaggagePropagator when using @sentry/bun

Bun version: v1.1.20

await Bun.build({
  entrypoints: ['src/main.ts'],
  outdir: 'dist',
  target: 'bun',
  sourcemap: 'external',
})

Btw, this probably happened when I added this:

import('reflect-metadata').then(() => {
  import('./server')
})

(The server file contains all the rest of the code, including @sentry/bun imports)

Same issue with llamaindex when using OpenAI.

import { OpenAI } from "llamaindex";
const openaiLLM = new OpenAI({ model: "gpt-4o-mini", temperature: 0.7 });

This was fixed in Bun v1.1.21. To get Bun v1.1.21, please run

bun upgrade

If you're still running into this issue, please leave a comment , we'll re-open it and @paperdave will investigate