oven-sh/bun · error · Error

Do not use CommonJS module.exports in ESM modules. Use…

Error message

Do not use CommonJS module.exports in ESM modules. Use `export default { ... }` instead. See src/js/README.md

What it means

Preprocessing check in bundle-modules.ts: the builtin module has no ESM export statement, and its source contains `module.exports`. Builtin modules are bundled as ESM, so CommonJS export style is rejected and the message points to src/js/README.md for the required style.

Solutions

  1. Replace `module.exports = { ... }` with `export default { ... }`
  2. Convert CommonJS exports to named ESM exports (`export function`, `export const`)
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/codegen/bundle-modules.ts:89 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oven-sh/bun@8c5296ac45 (2026-08-16). Data as JSON: /api/errors/dea2b0b698e3276d. Report an issue: GitHub.

Appendix: source

Thrown at src/codegen/bundle-modules.ts:89

      n--;
      await Bun.sleep(5);
    }
  }
  throw err;
}

const bunRepoRoot = path.join(CMAKE_BUILD_ROOT, "..", "..");

// Preprocess builtins
const bundledEntryPoints: string[] = [];
for (let i = 0; i < nativeStartIndex; i++) {
  try {
    const file = path.join(BASE, moduleList[i]);
    let input = fs.readFileSync(file, "utf8");

    if (!/\bexport\s+(?:function|class|const|default|{)/.test(input)) {
      if (input.includes("module.exports")) {
        throw new Error(
          "Do not use CommonJS module.exports in ESM modules. Use `export default { ... }` instead. See src/js/README.md",
        );
      } else {
        throw new Error(
          `Internal modules must have at least one ESM export statement in '${path.relative(bunRepoRoot, file)}' — see src/js/README.md`,
        );
      }
    }

    // TODO: there is no reason this cannot be converted automatically.
    // import { ... } from '...' -> `const { ... } = require('...')`
    const scannedImports = t.scan(input);
    for (const imp of scannedImports.imports) {
      if (imp.kind === "import-statement") {
        var isBuiltin = true;
        try {
          if (!builtinModules.includes(imp.path)) {
            requireTransformer(imp.path, moduleList[i]);

View on GitHub (pinned to 8c5296ac45)