{"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/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/01_require.js#L1892-L1928","documentation":"CommonJS Module#load() marks a module loaded after evaluation; calling load() again on the same Module instance throws 'Module already loaded'. require() normally shields you via the module cache, so this error signals cache bypass: manual Module#load calls, custom _load overrides, or hook code re-loading an existing instance.","triggerScenarios":"Fetching a cached module (Module._cache[filename]) and calling module.load(filename) on it; a patched Module._load that returns existing modules but still invokes load; double-loading inside registerHooks load implementations.","commonSituations":"Hand-rolled CJS interop in transpilers or bundlers; test harnesses that manipulate the module cache; monkey-patching Module internals for hot-reload style tooling.","solutions":["Check module.loaded before calling load(); skip if already true","Prefer Module._load() or require(), which consult the cache for you","If you genuinely need re-execution, construct a fresh Module and manage the cache yourself instead of re-loading the instance"],"exampleFix":"// before\nconst m = Module._cache[filename];\nm.load(filename); // Error: Module already loaded\n\n// after\nconst m = Module._cache[filename];\nif (m && !m.loaded) {\n  m.load(filename);\n}","handlingStrategy":"type-guard","validationCode":"const cached = Module._cache[filename];\nif (cached && !cached.loaded) {\n  cached.load(filename);\n} else if (!cached) {\n  const m = new Module(filename, null);\n  m.load(filename);\n}","typeGuard":"function isLoadableModule(m: unknown): m is { load(filename: string): void; loaded: boolean } {\n  return m != null && typeof m === \"object\" && \"loaded\" in m && \"load\" in m && !(m as { loaded: boolean }).loaded;\n}","tryCatchPattern":null,"preventionTips":["Prefer require()/Module._load() which handle caching correctly","Never call Module#load on objects you did not construct in the same code path","If you manipulate the cache, delete the entry before re-loading rather than reusing the instance"],"tags":["node-compat","commonjs","modules","cache","loaders"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}