oven-sh / bun

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

Error: `await` can only be used in `async` functions when using `--bytecode` during build #14655

Open mavyfaby opened 1 week ago

mavyfaby commented 1 week ago

What version of Bun is running?

1.1.31+e448c4cc3

What platform is your computer?

Linux 6.11.0-8-generic x86_64 x86_64

What steps can reproduce the bug?

Create a simple program that has top-level await:


async function sum(a: number, b: number) {
    return new Promise(resolve => resolve(a + b));
}

await sum(5, 5);

run it with (the problem is --bytecode):

bun build --compile --bytecode --sourcemap --minify tests.ts --outfile test

What is the expected behavior?

Should've compiled without errors.

What do you see instead?

Image

Additional information

No response

versecafe commented 1 week ago

@mavyfaby --bytecode forces to cjs which doesn't support top level await, you can check out a node thread on it https://github.com/nodejs/node/issues/21267 could change in the future but the difference in CJS vs ESM resolution cause the issue, you can always swap from top level await to a

new Promise((resolve, reject) => {
  resolve(true);
});