denoland/deno · error · TypeError
resolve hook must return
Error message
resolve hook must return { shortCircuit: true } or call nextResolve What it means
Deno's implementation of node:module's registerHooks validates every synchronous resolve hook: each hook must either call the provided nextResolve (chaining to the next hook or the default resolver) or return an object with shortCircuit: true, claiming final responsibility for the resolution. A hook that returns undefined or a plain result without doing either throws this TypeError at resolution time.
Solutions
- End every code path with return nextResolve(specifier, context, nextResolve)
- When you resolved the URL yourself, return { shortCircuit: true, url }
- Audit conditional branches and try/catch blocks in the hook for missing returns
Example fix
// before
registerHooks({
resolve(spec, ctx, next) {
if (spec.startsWith('virtual:')) {
return { url: 'file:///virtual/' + spec.slice(8) }; // missing shortCircuit
}
next(spec, ctx); // missing return
},
});
// after
registerHooks({
resolve(spec, ctx, next) {
if (spec.startsWith('virtual:')) {
return { shortCircuit: true, url: 'file:///virtual/' + spec.slice(8) };
}
return next(spec, ctx);
},
}); Defensive patterns
Strategy: validation
Validate before calling
function resolveHookWrapper(hook: Function) {
return (spec: string, ctx: unknown, next: Function) => {
let chained = false;
const wrappedNext = (...args: unknown[]) => { chained = true; return next(...args); };
const result = hook(spec, ctx, wrappedNext);
if (!chained && !result?.shortCircuit) {
throw new TypeError('resolve hook must return { shortCircuit: true } or call nextResolve');
}
return result;
};
} Type guard
function isValidResolveResult(r: unknown): r is { shortCircuit: true; url: string } {
return typeof r === 'object' && r !== null &&
(r as { shortCircuit?: unknown }).shortCircuit === true &&
typeof (r as { url?: unknown }).url === 'string';
} Try / catch
try {
registerHooks({ resolve });
require('./app');
} catch (e) {
if (e instanceof TypeError && e.message.includes('resolve hook')) {
// one of your hooks fell through: fix the hook, do not retry
throw new Error('custom resolve hook is missing a return/nextResolve on some path');
}
throw e;
} Prevention
- Default to `return nextResolve(...)` and only shortCircuit when you actually resolved the specifier
- Write a unit test per hook branch asserting every path returns or chains
- Enable strict TS checks on hook code so implicit-undefined returns are flagged
When it happens
Trigger: registerHooks({ resolve(spec, ctx, nextResolve) { /* no return */ } }); a hook with an early-return path (if/else, try/catch) that skips return nextResolve(spec, ctx, nextResolve); returning { url } without shortCircuit: true.
Common situations: Writing custom loaders for TS path aliases or virtual modules; porting hooks from the older async module.register() API; refactors that accidentally drop a return statement on one branch.
Related errors
- load hook must return
- A file exists at the destination
- A key and certificate are required for `Deno.listenTls`
- Cannot import scripts in a module worker
- EBADF
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/7a2876aedafb57d4.
Report an issue: GitHub.
Appendix: source
Thrown at ext/node/polyfills/01_require.js:757
try {
const resolved = new URL(spec, currentContext.parentURL).href;
return { url: resolved, shortCircuit: true };
} catch {
return { url: null, shortCircuit: true };
}
} finally {
insideResolveHook = false;
}
}
const hook = resolveHooks[index++];
let nextCalled = false;
const wrappedNext = (s, c) => {
nextCalled = true;
return nextResolve(s, c);
};
const result = hook(spec, currentContext, wrappedNext);
if (!nextCalled && !result?.shortCircuit) {
throw new TypeError(
"resolve hook must return { shortCircuit: true } or call nextResolve",
);
}
return result;
}
return nextResolve(specifier, context);
}
function esmResolveHookCallback(specifier, referrer, importAttributes) {
const attrs = { __proto__: null };
if (importAttributes !== null && typeof importAttributes === "object") {
for (const key in importAttributes) {
attrs[key] = importAttributes[key];
}
}
const context = {
parentURL: referrer || undefined,View on GitHub (pinned to 9ad36f7a2c)