{"record":{"id":"02d47bd502890b0d","repo":"denoland/deno","slug":"load-hook-must-return-shortcircuit-true-or-ca","errorCode":null,"errorMessage":"load hook must return { shortCircuit: true } or call nextLoad","messagePattern":"load hook must return (.+?) or call nextLoad","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/01_require.js","lineNumber":857,"sourceCode":"          // transient IO) falls through to Rust default loading via\n          // the load loop, which surfaces a clearer error if the\n          // module truly cannot be loaded.\n          return { source: null, shortCircuit: true };\n        }\n      }\n      // For other schemes (data:, http(s):, etc.) we cannot synchronously\n      // produce source here; fall through to Rust default loading.\n      return { source: null, shortCircuit: true };\n    }\n    const hook = loadHooks[index++];\n    let nextCalled = false;\n    const wrappedNext = (u, c) => {\n      nextCalled = true;\n      return nextLoad(u, c);\n    };\n    const result = hook(loadUrl, currentContext, wrappedNext);\n    if (!nextCalled && !result?.shortCircuit) {\n      throw new TypeError(\n        \"load hook must return { shortCircuit: true } or call nextLoad\",\n      );\n    }\n    return result;\n  }\n\n  const result = nextLoad(fileUrl, context);\n  return { result, effectiveUrl };\n}\n\nfunction _startEsmLoadLoop() {\n  if (esmLoadLoopRunning) return;\n  esmLoadLoopRunning = true;\n  (async () => {\n    while (true) {\n      const pollPromise = op_module_hooks_poll_load();\n      core.unrefOpPromise(pollPromise);\n      const req = await pollPromise;","sourceCodeStart":839,"sourceCodeEnd":875,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/01_require.js#L839-L875","documentation":"The same contract for the load stage of module.registerHooks: a load hook must call nextLoad or return an object with shortCircuit: true. Deno's own chain shows the pattern internally for data: URLs - return { source: null, shortCircuit: true }. A hook that falls off the end, or returns a source object without shortCircuit after bypassing nextLoad, throws this TypeError.","triggerScenarios":"registerHooks({ load(url, ctx, nextLoad) { if (!url.endsWith('.txt')) return; return { format: 'text' }; } }) - missing shortCircuit; returning the source string directly instead of { source, shortCircuit: true }; a guard clause that returns undefined.","commonSituations":"Custom loaders for non-JS assets (yaml, wasm, text, sql); hooks translated from the async register() API; forgetting shortCircuit when the hook itself supplies the transformed source.","solutions":["When the hook supplies the source, return { source, format, shortCircuit: true }","Otherwise always return nextLoad(url, context, nextLoad)","Check every guard clause returns one of the two valid shapes before shipping the hook"],"exampleFix":"// before\nregisterHooks({\n  load(url, ctx, next) {\n    if (url.endsWith('.yaml')) {\n      return { source: compileYaml(url), format: 'module' }; // missing shortCircuit\n    }\n    next(url, ctx); // missing return\n  },\n});\n\n// after\nregisterHooks({\n  load(url, ctx, next) {\n    if (url.endsWith('.yaml')) {\n      return { source: compileYaml(url), format: 'module', shortCircuit: true };\n    }\n    return next(url, ctx);\n  },\n});","handlingStrategy":"validation","validationCode":"function loadHookWrapper(hook: Function) {\n  return (url: string, ctx: unknown, next: Function) => {\n    let chained = false;\n    const wrappedNext = (...args: unknown[]) => { chained = true; return next(...args); };\n    const result = hook(url, ctx, wrappedNext);\n    if (!chained && !result?.shortCircuit) {\n      throw new TypeError('load hook must return { shortCircuit: true } or call nextLoad');\n    }\n    return result;\n  };\n}","typeGuard":"function isValidLoadResult(r: unknown): r is { shortCircuit: true; source?: string | null; format?: string } {\n  return typeof r === 'object' && r !== null &&\n    (r as { shortCircuit?: unknown }).shortCircuit === true;\n}","tryCatchPattern":"try {\n  registerHooks({ load });\n  import('./app.js');\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('load hook')) {\n    throw new Error('custom load hook is missing shortCircuit/nextLoad on some path');\n  }\n  throw e;\n}","preventionTips":["When the hook supplies transformed source, always include shortCircuit: true in the returned object","Cover every guard clause with a return - one bare `if (...) return;` path breaks loading","Test hooks against a fixture tree that exercises each branch of the hook"],"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"}