denoland/deno · error · Error
Module already loaded
Error message
Module already loaded
What it means
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.
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
Example fix
// before (hook re-entering the loader)
registerHooks({
load(url, ctx, next) {
require(new URL(url).pathname); // re-entrant require of the same file
return next(url, ctx);
},
});
// after (pure hook)
registerHooks({
load(url, ctx, next) {
return next(url, ctx); // transform the returned source instead
},
}); Defensive patterns
Strategy: try-catch
Validate before calling
function loadOnce(mod: { loaded: boolean; load(f: string): void }, filename: string): void {
if (mod.loaded) return; // idempotent guard for code driving Module internals
mod.load(filename);
} Type guard
function isUnloadedModule(m: unknown): m is { loaded: false; load(f: string): void } {
return typeof m === 'object' && m !== null && (m as { loaded?: unknown }).loaded === false;
} Try / catch
try {
require(specifier);
} catch (e) {
if (e instanceof Error && e.message === 'Module already loaded') {
// re-entrant loading (usually a hook requiring its own target); report and fix the hook
throw new Error(`re-entrant load of '${specifier}' - check load hooks for require()`);
}
throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/01d8e632240d3e26.
Report an issue: GitHub.
Appendix: source
Thrown at ext/node/polyfills/01_require.js:1910
* as `ts-node/register`.
* @param {string[]} requests List of modules to preload
*/
Module._preloadModules = function (requests) {
if (!ArrayIsArray(requests) || requests.length === 0) {
return;
}
const parent = new Module("internal/preload", null);
// All requested files must be resolved against cwd
parent.paths = Module._nodeModulePaths(process.cwd());
for (let i = 0; i < requests.length; i++) {
parent.require(requests[i]);
}
};
Module.prototype.load = function (filename) {
if (this.loaded) {
throw new Error("Module already loaded");
}
// Canonicalize the path so it's not pointing to the symlinked directory
// in `node_modules` directory of the referrer.
// When load hooks are active, the file may not exist on disk (virtual
// modules), so we fall back to the original filename.
let hasLoadHooks = false;
if (hookEntries.length > 0 && !insideLoadHook) {
for (let i = 0; i < hookEntries.length; i++) {
if (hookEntries[i].load !== null) {
hasLoadHooks = true;
break;
}
}
}
if (hasLoadHooks) {
try {
this.filename = op_require_real_path(filename);View on GitHub (pinned to 9ad36f7a2c)