{"record":{"id":"7a2876aedafb57d4","repo":"denoland/deno","slug":"resolve-hook-must-return-shortcircuit-true-or","errorCode":null,"errorMessage":"resolve hook must return { shortCircuit: true } or call nextResolve","messagePattern":"resolve hook must return (.+?) or call nextResolve","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/01_require.js","lineNumber":757,"sourceCode":"        try {\n          const resolved = new URL(spec, currentContext.parentURL).href;\n          return { url: resolved, shortCircuit: true };\n        } catch {\n          return { url: null, shortCircuit: true };\n        }\n      } finally {\n        insideResolveHook = false;\n      }\n    }\n    const hook = resolveHooks[index++];\n    let nextCalled = false;\n    const wrappedNext = (s, c) => {\n      nextCalled = true;\n      return nextResolve(s, c);\n    };\n    const result = hook(spec, currentContext, wrappedNext);\n    if (!nextCalled && !result?.shortCircuit) {\n      throw new TypeError(\n        \"resolve hook must return { shortCircuit: true } or call nextResolve\",\n      );\n    }\n    return result;\n  }\n\n  return nextResolve(specifier, context);\n}\n\nfunction esmResolveHookCallback(specifier, referrer, importAttributes) {\n  const attrs = { __proto__: null };\n  if (importAttributes !== null && typeof importAttributes === \"object\") {\n    for (const key in importAttributes) {\n      attrs[key] = importAttributes[key];\n    }\n  }\n  const context = {\n    parentURL: referrer || undefined,","sourceCodeStart":739,"sourceCodeEnd":775,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/01_require.js#L739-L775","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nregisterHooks({\n  resolve(spec, ctx, next) {\n    if (spec.startsWith('virtual:')) {\n      return { url: 'file:///virtual/' + spec.slice(8) }; // missing shortCircuit\n    }\n    next(spec, ctx); // missing return\n  },\n});\n\n// after\nregisterHooks({\n  resolve(spec, ctx, next) {\n    if (spec.startsWith('virtual:')) {\n      return { shortCircuit: true, url: 'file:///virtual/' + spec.slice(8) };\n    }\n    return next(spec, ctx);\n  },\n});","handlingStrategy":"validation","validationCode":"function resolveHookWrapper(hook: Function) {\n  return (spec: string, ctx: unknown, next: Function) => {\n    let chained = false;\n    const wrappedNext = (...args: unknown[]) => { chained = true; return next(...args); };\n    const result = hook(spec, ctx, wrappedNext);\n    if (!chained && !result?.shortCircuit) {\n      throw new TypeError('resolve hook must return { shortCircuit: true } or call nextResolve');\n    }\n    return result;\n  };\n}","typeGuard":"function isValidResolveResult(r: unknown): r is { shortCircuit: true; url: string } {\n  return typeof r === 'object' && r !== null &&\n    (r as { shortCircuit?: unknown }).shortCircuit === true &&\n    typeof (r as { url?: unknown }).url === 'string';\n}","tryCatchPattern":"try {\n  registerHooks({ resolve });\n  require('./app');\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('resolve hook')) {\n    // one of your hooks fell through: fix the hook, do not retry\n    throw new Error('custom resolve hook is missing a return/nextResolve on some path');\n  }\n  throw e;\n}","preventionTips":["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"],"tags":["deno","node-compat","esm","module-hooks","typeerror"],"backgroundTag":"module-loader-hook-contract","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"}