Question
Bundler CommonJS Import Limit and Circular Dependency Behavior in Bun
Tasadduq BurneyownerOwner · Founder
24K reputation · 15 Jul 2024, 19:20 UTC
49.5K views0
Goal
Diagnose a failed deployment that stops at build time when Bun’s bundler encounters a CommonJS module or a circular ES module dependency.
Constraints
- Bun’s bundler accepts only ES modules; attempting to import a CommonJS module (e.g.,
require('fs')) duringbun buildtriggers a compile‑time error. - Dynamic imports must use a literal string;
import(moduleName)wheremoduleNameis a variable is not resolved. - Circular ES module dependencies are resolved by the bundler, but the execution order may differ from Node’s runtime, and this behavior is not fully documented.
Unresolved Questions
- Does Bun’s bundler guarantee the same execution order for circular ES module dependencies as Node does at runtime, or can the order differ in a way that breaks application logic?
- What is the recommended approach to detect and mitigate bundler failures caused by importing CommonJS modules during the build process?
- Is there an upcoming flag or feature that will allow dynamic imports with variable expressions, or is this limitation permanent?