oven-sh/bun · error · Error

Builtin Bundler: do not use import.meta.require() (in ${file

Error message

Builtin Bundler: do not use import.meta.require() (in ${file_path}))

What it means

Bun's builtin bundler post-processes each bundled src/js module with a regex pipeline, and this error fires when the captured output still contains an import.meta.require(...) call. Builtin modules are embedded into the binary as captured function bodies, so import.meta.require — which depends on a real file location and loader context — cannot work there. It is a hard source-code policy violation, not an environment issue.

Source

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

mark("Bundle modules");

const outputs = new Map();

for (const entrypoint of bundledEntryPoints) {
  const file_path = entrypoint.slice(TMP_DIR.length + 1).replace(/\.ts$/, ".js");
  const file = Bun.file(path.join(TMP_DIR, "modules_out", file_path));
  const output = await file.text();
  let captured = `(function (){${output.replace("// @bun\n", "").trim()}})`;
  let usesDebug = output.includes("$debug_log");
  let usesAssert = output.includes("$assert");
  captured =
    captured
      .replace(/\$\$EXPORT\$\$\((.*)\).\$\$EXPORT_END\$\$;/, "return $1")
      .replace(/]\s*,\s*__(debug|assert)_end__\)/g, ")")
      .replace(/]\s*,\s*__debug_end__\)/g, ")")
      .replace(/import.meta.require\((.*?)\)/g, (expr, specifier) => {
        throw new Error(`Builtin Bundler: do not use import.meta.require() (in ${file_path}))`);
      })
      .replace(/return \$\nexport /, "return")
      .replace(/__intrinsic__/g, "@")
      .replace(/__no_intrinsic__/g, "") + "\n";
  captured = captured.replace(
    /function\s*\(.*?\)\s*{/,
    '$&"use strict";' +
      (usesDebug
        ? createLogClientJS(
            file_path.replace(".js", ""),
            idToPublicSpecifierOrEnumName(file_path).replace(/^node:|^bun:/, ""),
          )
        : "") +
      (usesAssert ? createAssertClientJS(idToPublicSpecifierOrEnumName(file_path).replace(/^node:|^bun:/, "")) : ""),
  );
  const errors = [...captured.matchAll(/@bundleError\((.*)\)/g)];
  if (errors.length) {
    throw new Error(`Errors in ${entrypoint}:\n${errors.map(x => x[1]).join("\n")}`);

View on GitHub (pinned to 8c5296ac45)

Solutions

  1. Find the call site: the error names the offending file (in ${file_path}); grep that module for `import.meta.require`
  2. Replace it with a static ESM `import` at the top of the module
  3. If the require must be lazy, restructure to a dynamic `import()` or hoist the dependency so the bundler resolves it at build time

Example fix

// before (src/js/some/module.ts)
const fs = import.meta.require("node:fs");

// after
import fs from "node:fs";
Defensive patterns

Strategy: validation

Validate before calling

// pre-commit / pre-build check: no import.meta.require in embedded builtin sources
import { Glob } from "bun";
const offenders: string[] = [];
for (const f of new Glob("src/js/**/*.{ts,tsx,js,mjs}").scanSync(".")) {
  if ((await Bun.file(f).text()).includes("import.meta.require")) offenders.push(f);
}
if (offenders.length) throw new Error(`import.meta.require forbidden in: ${offenders}`);

Prevention

When it happens

Trigger: Adding or editing a module under src/js/ (or src/thirdparty/, src/node/) that calls import.meta.require(...), then running the builtin-modules codegen step of `bun run build`.

Common situations: Contributors porting Node code that uses import.meta.require for lazy requires; copy-pasting patterns that work in userland bundling but are forbidden inside Bun's embedded builtins.

Related errors


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