denoland/deno · error · TypeError
Invalid type for fetch: must be a function
Error message
Invalid type for fetch: must be a function
What it means
`deno serve` (the declarative-server path of `Deno.serve`) inspects the entrypoint module's exports via `registerDeclarativeServer`. If the module has an own `fetch` binding, it must be a function, because Deno dispatches every incoming request by calling `exports.fetch(request)`. Any other type (string, object, class instance, re-exported variable) fails this check synchronously at startup, before the server listens.
Solutions
- Make the export a function: `export function fetch(request: Request): Response | Promise<Response>`
- If you also need config values, keep them under different names and export only a function named `fetch`
- Run `deno check` against the entrypoint so bad export shapes fail in CI, not at serve time
Example fix
// before
export const fetch = router; // router is an object
// after
export function fetch(request: Request): Promise<Response> {
return router.handle(request);
} Defensive patterns
Strategy: validation
Validate before calling
// smoke-test the entrypoint before `deno serve`
const m = await import("./main.ts");
if (Object.hasOwn(m, "fetch") && typeof m.fetch !== "function") {
throw new Error("entrypoint 'fetch' export must be a function");
} Type guard
type ServeEntrypoint = {
fetch: (req: Request) => Response | Promise<Response>;
onListen?: (addr: { hostname: string; port: number }) => void;
};
const isServeEntrypoint = (m: object): m is ServeEntrypoint =>
typeof (m as ServeEntrypoint).fetch === "function"; Prevention
- Run `deno check` on the entrypoint in CI so export shapes fail before deploy
- Keep exactly one export named `fetch`, and keep it a function
- Name configuration constants anything other than `fetch`/`onListen`
When it happens
Trigger: `deno serve main.ts` where main.ts contains `export const fetch = ...` with a non-function value, or re-exports a non-function binding named `fetch`, or exports a router/config object under the name `fetch`.
Common situations: Porting Cloudflare-Workers-style entrypoints where `fetch` is the convention but the export got shadowed by a constant; refactors that turn the handler into a config object; bundlers renaming or transforming the export.
Related errors
- Invalid type for onListen: must be a function
- Return value from serve handler must not be an error…
- An executable name was not provided. One could not be…
- app name resolves its to , which is already part of the…
- Blob URL fetch only supports GET method
AI-assisted analysis of denoland/deno@abd22074e4 (2026-08-20).
Data as JSON: /api/errors/8e5eaf2c1f664f4e.
Report an issue: GitHub.
Appendix: source
Thrown at ext/http/00_serve.ts:1663
},
[SymbolAsyncDispose]() {
return this.shutdown();
},
};
}
internals.addTrailers = addTrailers;
internals.serveHttpOnListener = serveHttpOnListener;
internals.serveHttpOnConnection = serveHttpOnConnection;
internals.resetLegacyAbortWarning = () => {
legacyAbortWarned = false;
};
function registerDeclarativeServer(exports) {
if (!ObjectHasOwn(exports, "fetch")) return;
if (typeof exports.fetch !== "function") {
throw new TypeError("Invalid type for fetch: must be a function");
}
if (
exports.onListen !== undefined && typeof exports.onListen !== "function"
) {
throw new TypeError("Invalid type for onListen: must be a function");
}
return ({
servePort,
serveHost,
workerCountWhenMain,
}) => {
const server = Deno.serve({
port: servePort,
hostname: serveHost,
[kLoadBalanced]: workerCountWhenMain == null
? trueView on GitHub (pinned to abd22074e4)