{"record":{"id":"8e5eaf2c1f664f4e","repo":"denoland/deno","slug":"invalid-type-for-fetch-must-be-a-function","errorCode":null,"errorMessage":"Invalid type for fetch: must be a function","messagePattern":"Invalid type for fetch: must be a function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/http/00_serve.ts","lineNumber":1663,"sourceCode":"    },\n    [SymbolAsyncDispose]() {\n      return this.shutdown();\n    },\n  };\n}\n\ninternals.addTrailers = addTrailers;\ninternals.serveHttpOnListener = serveHttpOnListener;\ninternals.serveHttpOnConnection = serveHttpOnConnection;\ninternals.resetLegacyAbortWarning = () => {\n  legacyAbortWarned = false;\n};\n\nfunction registerDeclarativeServer(exports) {\n  if (!ObjectHasOwn(exports, \"fetch\")) return;\n\n  if (typeof exports.fetch !== \"function\") {\n    throw new TypeError(\"Invalid type for fetch: must be a function\");\n  }\n\n  if (\n    exports.onListen !== undefined && typeof exports.onListen !== \"function\"\n  ) {\n    throw new TypeError(\"Invalid type for onListen: must be a function\");\n  }\n\n  return ({\n    servePort,\n    serveHost,\n    workerCountWhenMain,\n  }) => {\n    const server = Deno.serve({\n      port: servePort,\n      hostname: serveHost,\n      [kLoadBalanced]: workerCountWhenMain == null\n        ? true","sourceCodeStart":1645,"sourceCodeEnd":1681,"githubUrl":"https://github.com/denoland/deno/blob/abd22074e47c6a5cd14e9e4e84743f084aa5a575/ext/http/00_serve.ts#L1645-L1681","documentation":"`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.","triggerScenarios":"`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`.","commonSituations":"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.","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"],"exampleFix":"// before\nexport const fetch = router; // router is an object\n\n// after\nexport function fetch(request: Request): Promise<Response> {\n  return router.handle(request);\n}","handlingStrategy":"validation","validationCode":"// smoke-test the entrypoint before `deno serve`\nconst m = await import(\"./main.ts\");\nif (Object.hasOwn(m, \"fetch\") && typeof m.fetch !== \"function\") {\n  throw new Error(\"entrypoint 'fetch' export must be a function\");\n}","typeGuard":"type ServeEntrypoint = {\n  fetch: (req: Request) => Response | Promise<Response>;\n  onListen?: (addr: { hostname: string; port: number }) => void;\n};\nconst isServeEntrypoint = (m: object): m is ServeEntrypoint =>\n  typeof (m as ServeEntrypoint).fetch === \"function\";","tryCatchPattern":null,"preventionTips":["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`"],"tags":["deno-serve","cli","module-exports","fetch"],"backgroundTag":"invalid-module-export","analyzedSha":"abd22074e47c6a5cd14e9e4e84743f084aa5a575","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}