#Import errors when moving from webpack to bun bundling

1 messages · Page 1 of 1 (latest)

steep talon
#

Hi, I'm moving to Bun bundling instead of my current webpack bundling at my frontend application. I encountered an error when trying to serve the dist, the error is

Uncaught TypeError: import_W3CEBNF.default.Parser is not a constructor

It comes from the following code in one of my dependencies dependencies

import EBNF from "ebnf/dist/Grammars/W3CEBNF";
import { jsonQueryGrammar } from "./jsonQueryGrammar";
...
const parser = new EBNF.Parser(jsonQueryGrammar);

(The dependencies are [email protected] > @sagold/[email protected])
I assume it is some sort of ESM / CJS error, but I have no idea how to fix it, maybe its a configuration issue, or if it's even not fixable and I need to use a replacement package?

My tsconfig is (based on the recommended one in the docs):
tsconfig

The build I do via code:

import { build } from "bun";

console.log("Building client...");
build({
    experimentalCss: true,
    entrypoints: ["./src/index.ts", "./src/okta.index.ts"],
    outdir: "./dist",
    sourcemap: "inline",
})
    .then(console.log)
    .catch(error => console.error(error));

versions of stuff:

Bun: 1.1.38
Typescript: 5.7.2
OS: Ubuntu 22.04.5 LTS (WSL)

Also, if you have any general tips and common errors when moving from webpack to bun I would love to hear them!

round gust
#

slightly more minimal reproduction:

import EBNF from "ebnf/dist/Grammars/W3CEBNF";
new EBNF.Parser().parse("a ::= b | c");
#

esbuild has the same bug

#

the cause is the file is defining __esModule and then setting the default export to be the value instead of module.exports = BNF

#

this is in tsc

#

sorry, none of this helps you workaround the bug

#

interestingly, we handle this correctly at runtime.