denoland/deno · error · TypeError

load hook must return

Error message

load hook must return { shortCircuit: true } or call nextLoad

What it means

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.

Solutions

  1. When the hook supplies the source, return { source, format, shortCircuit: true }
  2. Otherwise always return nextLoad(url, context, nextLoad)
  3. Check every guard clause returns one of the two valid shapes before shipping the hook

Example fix

// before
registerHooks({
  load(url, ctx, next) {
    if (url.endsWith('.yaml')) {
      return { source: compileYaml(url), format: 'module' }; // missing shortCircuit
    }
    next(url, ctx); // missing return
  },
});

// after
registerHooks({
  load(url, ctx, next) {
    if (url.endsWith('.yaml')) {
      return { source: compileYaml(url), format: 'module', shortCircuit: true };
    }
    return next(url, ctx);
  },
});
Defensive patterns

Strategy: validation

Validate before calling

function loadHookWrapper(hook: Function) {
  return (url: string, ctx: unknown, next: Function) => {
    let chained = false;
    const wrappedNext = (...args: unknown[]) => { chained = true; return next(...args); };
    const result = hook(url, ctx, wrappedNext);
    if (!chained && !result?.shortCircuit) {
      throw new TypeError('load hook must return { shortCircuit: true } or call nextLoad');
    }
    return result;
  };
}

Type guard

function isValidLoadResult(r: unknown): r is { shortCircuit: true; source?: string | null; format?: string } {
  return typeof r === 'object' && r !== null &&
    (r as { shortCircuit?: unknown }).shortCircuit === true;
}

Try / catch

try {
  registerHooks({ load });
  import('./app.js');
} catch (e) {
  if (e instanceof TypeError && e.message.includes('load hook')) {
    throw new Error('custom load hook is missing shortCircuit/nextLoad on some path');
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/02d47bd502890b0d. Report an issue: GitHub.

Appendix: source

Thrown at ext/node/polyfills/01_require.js:857

          // transient IO) falls through to Rust default loading via
          // the load loop, which surfaces a clearer error if the
          // module truly cannot be loaded.
          return { source: null, shortCircuit: true };
        }
      }
      // For other schemes (data:, http(s):, etc.) we cannot synchronously
      // produce source here; fall through to Rust default loading.
      return { source: null, shortCircuit: true };
    }
    const hook = loadHooks[index++];
    let nextCalled = false;
    const wrappedNext = (u, c) => {
      nextCalled = true;
      return nextLoad(u, c);
    };
    const result = hook(loadUrl, currentContext, wrappedNext);
    if (!nextCalled && !result?.shortCircuit) {
      throw new TypeError(
        "load hook must return { shortCircuit: true } or call nextLoad",
      );
    }
    return result;
  }

  const result = nextLoad(fileUrl, context);
  return { result, effectiveUrl };
}

function _startEsmLoadLoop() {
  if (esmLoadLoopRunning) return;
  esmLoadLoopRunning = true;
  (async () => {
    while (true) {
      const pollPromise = op_module_hooks_poll_load();
      core.unrefOpPromise(pollPromise);
      const req = await pollPromise;

View on GitHub (pinned to 9ad36f7a2c)