{"record":{"id":"01d8e632240d3e26","repo":"denoland/deno","slug":"module-already-loaded","errorCode":null,"errorMessage":"Module already loaded","messagePattern":"Module already loaded","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/01_require.js","lineNumber":1910,"sourceCode":" * as `ts-node/register`.\n * @param {string[]} requests List of modules to preload\n */\nModule._preloadModules = function (requests) {\n  if (!ArrayIsArray(requests) || requests.length === 0) {\n    return;\n  }\n\n  const parent = new Module(\"internal/preload\", null);\n  // All requested files must be resolved against cwd\n  parent.paths = Module._nodeModulePaths(process.cwd());\n  for (let i = 0; i < requests.length; i++) {\n    parent.require(requests[i]);\n  }\n};\n\nModule.prototype.load = function (filename) {\n  if (this.loaded) {\n    throw new Error(\"Module already loaded\");\n  }\n\n  // Canonicalize the path so it's not pointing to the symlinked directory\n  // in `node_modules` directory of the referrer.\n  // When load hooks are active, the file may not exist on disk (virtual\n  // modules), so we fall back to the original filename.\n  let hasLoadHooks = false;\n  if (hookEntries.length > 0 && !insideLoadHook) {\n    for (let i = 0; i < hookEntries.length; i++) {\n      if (hookEntries[i].load !== null) {\n        hasLoadHooks = true;\n        break;\n      }\n    }\n  }\n  if (hasLoadHooks) {\n    try {\n      this.filename = op_require_real_path(filename);","sourceCodeStart":1892,"sourceCodeEnd":1928,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/01_require.js#L1892-L1928","documentation":"Module.prototype.load in Deno's CJS polyfill refuses to load a module whose loaded flag is already set, throwing this generic Error. In normal require flow the flag prevents double execution, so seeing it means re-entrant loading: typically a load hook that synchronously requires the very module being loaded, or code invoking Module internals directly.","triggerScenarios":"A module.registerHooks load hook whose body calls require() on the file it is currently loading; manually calling cachedModule.load(filename) twice; circular requires combined with hook-driven loading that re-enters Module.prototype.load.","commonSituations":"Bundler/transform hooks that require the target to inspect side effects; scripts pulling Module._load or Module internals from node:module; polyfill interop bugs in older Deno builds.","solutions":["Make load hooks pure: transform and return source, never require the module being loaded","Break require cycles with a lazy require inside the function that needs the dependency","When using Module internals manually, check mod.loaded before calling load"],"exampleFix":"// before (hook re-entering the loader)\nregisterHooks({\n  load(url, ctx, next) {\n    require(new URL(url).pathname); // re-entrant require of the same file\n    return next(url, ctx);\n  },\n});\n\n// after (pure hook)\nregisterHooks({\n  load(url, ctx, next) {\n    return next(url, ctx); // transform the returned source instead\n  },\n});","handlingStrategy":"try-catch","validationCode":"function loadOnce(mod: { loaded: boolean; load(f: string): void }, filename: string): void {\n  if (mod.loaded) return; // idempotent guard for code driving Module internals\n  mod.load(filename);\n}","typeGuard":"function isUnloadedModule(m: unknown): m is { loaded: false; load(f: string): void } {\n  return typeof m === 'object' && m !== null && (m as { loaded?: unknown }).loaded === false;\n}","tryCatchPattern":"try {\n  require(specifier);\n} catch (e) {\n  if (e instanceof Error && e.message === 'Module already loaded') {\n    // re-entrant loading (usually a hook requiring its own target); report and fix the hook\n    throw new Error(`re-entrant load of '${specifier}' - check load hooks for require()`);\n  }\n  throw e;\n}","preventionTips":["Keep registerHooks load hooks pure - never require() the module being loaded inside the hook","Avoid calling Module._load/Module.prototype.load internals directly","Break circular requires with deferred (in-function) require calls"],"tags":["deno","node-compat","commonjs","require","internal-error"],"backgroundTag":"module-already-loaded","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}