{"record":{"id":"6ced58b60aa66d49","repo":"oven-sh/bun","slug":"builtin-bundler-do-not-use-import-meta-require","errorCode":null,"errorMessage":"Builtin Bundler: do not use import.meta.require() (in ${file_path}))","messagePattern":"Builtin Bundler: do not use import\\.meta\\.require\\(\\) \\(in (.+?)\\)\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/codegen/bundle-modules.ts","lineNumber":261,"sourceCode":"\nmark(\"Bundle modules\");\n\nconst outputs = new Map();\n\nfor (const entrypoint of bundledEntryPoints) {\n  const file_path = entrypoint.slice(TMP_DIR.length + 1).replace(/\\.ts$/, \".js\");\n  const file = Bun.file(path.join(TMP_DIR, \"modules_out\", file_path));\n  const output = await file.text();\n  let captured = `(function (){${output.replace(\"// @bun\\n\", \"\").trim()}})`;\n  let usesDebug = output.includes(\"$debug_log\");\n  let usesAssert = output.includes(\"$assert\");\n  captured =\n    captured\n      .replace(/\\$\\$EXPORT\\$\\$\\((.*)\\).\\$\\$EXPORT_END\\$\\$;/, \"return $1\")\n      .replace(/]\\s*,\\s*__(debug|assert)_end__\\)/g, \")\")\n      .replace(/]\\s*,\\s*__debug_end__\\)/g, \")\")\n      .replace(/import.meta.require\\((.*?)\\)/g, (expr, specifier) => {\n        throw new Error(`Builtin Bundler: do not use import.meta.require() (in ${file_path}))`);\n      })\n      .replace(/return \\$\\nexport /, \"return\")\n      .replace(/__intrinsic__/g, \"@\")\n      .replace(/__no_intrinsic__/g, \"\") + \"\\n\";\n  captured = captured.replace(\n    /function\\s*\\(.*?\\)\\s*{/,\n    '$&\"use strict\";' +\n      (usesDebug\n        ? createLogClientJS(\n            file_path.replace(\".js\", \"\"),\n            idToPublicSpecifierOrEnumName(file_path).replace(/^node:|^bun:/, \"\"),\n          )\n        : \"\") +\n      (usesAssert ? createAssertClientJS(idToPublicSpecifierOrEnumName(file_path).replace(/^node:|^bun:/, \"\")) : \"\"),\n  );\n  const errors = [...captured.matchAll(/@bundleError\\((.*)\\)/g)];\n  if (errors.length) {\n    throw new Error(`Errors in ${entrypoint}:\\n${errors.map(x => x[1]).join(\"\\n\")}`);","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/src/codegen/bundle-modules.ts#L243-L279","documentation":"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.","triggerScenarios":"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`.","commonSituations":"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.","solutions":["Find the call site: the error names the offending file (in ${file_path}); grep that module for `import.meta.require`","Replace it with a static ESM `import` at the top of the module","If the require must be lazy, restructure to a dynamic `import()` or hoist the dependency so the bundler resolves it at build time"],"exampleFix":"// before (src/js/some/module.ts)\nconst fs = import.meta.require(\"node:fs\");\n\n// after\nimport fs from \"node:fs\";","handlingStrategy":"validation","validationCode":"// pre-commit / pre-build check: no import.meta.require in embedded builtin sources\nimport { Glob } from \"bun\";\nconst offenders: string[] = [];\nfor (const f of new Glob(\"src/js/**/*.{ts,tsx,js,mjs}\").scanSync(\".\")) {\n  if ((await Bun.file(f).text()).includes(\"import.meta.require\")) offenders.push(f);\n}\nif (offenders.length) throw new Error(`import.meta.require forbidden in: ${offenders}`);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["In src/js builtin modules always use static ESM imports or dynamic import()","Treat this error as a compile error in review: never commit import.meta.require under src/js"],"tags":["bundler","builtins","codegen","static-analysis"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}