bun
bun copied to clipboard
bun build --compile with puppeteer. but bun run works well
What version of Bun is running?
0.8.1
What platform is your computer?
Darwin 22.5.0 arm64 arm
What steps can reproduce the bug?
a.js
import fs from 'node:fs';
import os from 'node:os';
import puppeteer from "puppeteer";
console.log(111)
bun add puppeteer
package.json
{ "dependencies": { "puppeteer": "^21.1.1" } }
bun build a.js --compile
./a
What is the expected behavior?
111
What do you see instead?
53899 | }
53900 | }
53901 | }
53902 |
53903 |
53904 | class CDPClientAdapter extends BidiMapper.EventEmitter {
^
TypeError: undefined is not an object (evaluating 'BidiMapper.EventEmitter')
at compiled://root/a:53904:31
Additional information
bun run works well
Is there any solution for this problem yet? I'm facing the same issue.
If I use require instead of import, the code works.
Working code,
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("https://example.com/");
console.log(await page.title());
await browser.close();
})();
Command line,
bun build scrape.js --outfile scrape --target node --compile
Non working code,
import puppeteer from "puppeteer";
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("https://example.com/");
console.log(await page.title());
await browser.close();
})();
Error,
./scrape
17661 | adapter.close();
17662 | }
17663 | }
17664 | }
17665 |
17666 | class CDPClientAdapter extends BidiMapper.EventEmitter {
^
TypeError: undefined is not an object (evaluating 'BidiMapper.EventEmitter')
at /$bunfs/root/scrape:17666:32
1.1.3 the same problem
I can confirm that this still is an issue in bun version 1.1.3
Same here, still broken on 1.1.4
Still broken on v1.1.6
Still broken on v1.1.10
As stated before this is probably caused by the compiler. Because when I convert or transpile the project to cjs and then try to bundle it with bun, it works nicely. There was a swc plugin as well but it's archived, but I think we can improvise something like this.
Example code:
import puppeteer from "puppeteer";
console.log('launching')
const browser = await puppeteer.launch();
const page = await browser.newPage();
console.log('navigating')
await page.goto("https://example.com");
await page.screenshot({ path: "example.png" });
console.log('closing')
await browser.close();
Example commands:
# install the things
bun install @swc/cli @swc/core
# use swc to convert this to cjs
bun swc index.ts -C module.type=commonjs -o index.js
# now use bun to create a single executable
bun build index.js --target=bun --compile --outfile index --minify
# run it and done!
./index
Sample output:
➜ bun swc index.ts -C module.type=commonjs -o index.js
Successfully compiled 1 file with swc.
➜ bun build index.js --target=bun --compile --outfile index --minify
[127ms] minify -7.90 MB (estimate)
[33ms] bundle 564 modules
[47ms] compile index
➜ ls -lah index
-rwxrwxrwx 1 developer developer 99M Jun 9 11:12 index
➜ ./index
launching
navigating
closing
Unless this is fixed from the bun, this is kind of alternative solution for me for now.
Side note: The swc is way much faster with bun than node for this example. Almost 9-10x faster.
hyperfine "bun swc index.ts -C module.type=commonjs -o index.js"
Benchmark 1: bun swc index.ts -C module.type=commonjs -o index.js
Time (mean ± σ): 53.4 ms ± 1.9 ms [User: 48.7 ms, System: 23.1 ms]
Range (min … max): 51.6 ms … 62.0 ms 48 runs
hyperfine "pnpm swc index.ts -C module.type=commonjs -o index.js"
Benchmark 1: pnpm swc index.ts -C module.type=commonjs -o index.js
Time (mean ± σ): 466.7 ms ± 11.0 ms [User: 224.7 ms, System: 57.6 ms]
Range (min … max): 452.4 ms … 486.0 ms 10 runs