oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
72.83k stars 2.64k forks source link

bun build fails: The superclass is not a constructor #8925

Closed laurensV closed 3 weeks ago

laurensV commented 6 months ago

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 (https://github.com/oven-sh/bun/issues/6261) and AWS CDK (https://github.com/oven-sh/bun/issues/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

tim-brand commented 6 months ago

I got the same issue, with the puppeteer package.

toondkn commented 4 months ago

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

toondkn commented 4 months ago

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.

toondkn commented 4 months ago

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.

liquidg3 commented 3 months ago

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
avarayr commented 3 months ago

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
nephix commented 2 months ago

I got the same issue when using jsx-email (https://github.com/shellscape/jsx-email/issues/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
risen228 commented 1 month ago

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)

kaitakami commented 3 weeks ago

Same issue with llamaindex when using OpenAI.

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

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