{"record":{"id":"4ea97a9b0b7d3a36","repo":"remix-run/remix","slug":"no-router-found-in-request-context","errorCode":null,"errorMessage":"No router found in request context.","messagePattern":"No router found in request context\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/fetch-router/src/lib/request-context.ts","lineNumber":344,"sourceCode":"      )\n    }\n\n    this.#contextProperties.set(property, key)\n    this.#contextPropertyKeys.set(key, property)\n\n    Object.defineProperty(this, property, {\n      get: () => this.get(key),\n    })\n  }\n\n  #router?: Router<any>\n\n  /**\n   * The router handling this request.\n   */\n  get router(): Router<RequestContext<any, entries>> {\n    if (this.#router == null) {\n      throw new Error('No router found in request context.')\n    }\n\n    return this.#router as Router<RequestContext<any, entries>>\n  }\n\n  set router(router: Router<any>) {\n    this.#router = router\n  }\n\n  /**\n   * The URL of the current request.\n   */\n  url: URL\n}\n\nexport interface RequestContext<\n  params extends Record<string, any> = {},\n  entries extends ContextEntries = [],","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/remix-run/remix/blob/9696913134be3a4423513d2775f7b31d6917c049/packages/fetch-router/src/lib/request-context.ts#L326-L362","documentation":"RequestContext.router returns the router handling the request, but in some contexts (unit tests, direct construction, middleware running outside the router pipeline) no router was assigned. This getter throws instead of returning undefined so misuse fails loudly.","triggerScenarios":"Instantiating RequestContext manually (e.g. new RequestContext(request)) in tests and then accessing ctx.router; accessing router in code detached from the routing pipeline.","commonSituations":"Unit-testing middleware or route handlers with a hand-built context; helpers reused in scripts/tests where no router exists.","solutions":["In tests, construct the context through the router's test helpers or stub a router","Check 'router' in ctx / optional access before use if the API supports it","Restructure code so router access happens only inside route handlers"],"exampleFix":"// before\nconst ctx = new RequestContext(request)\nctx.router // throws\n// after\nconst router = createRouter(routes)\nconst ctx = router.createContext?.(request) ?? new RequestContext(request, { router })\nctx.router // ok","handlingStrategy":"try-catch","validationCode":"// in tests, build context through the router or pass one\nconst ctx = new RequestContext(request, { router })","typeGuard":"const hasRouter = (ctx: RequestContext): boolean => (ctx as any).#router != null","tryCatchPattern":"let router: Router | undefined\ntry { router = ctx.router } catch { router = undefined }","preventionTips":["Use the router's own helpers to build test contexts","Only access ctx.router inside real route handlers"],"tags":["request-context","fetch-router","testing"],"backgroundTag":"missing-router-context","analyzedSha":"9696913134be3a4423513d2775f7b31d6917c049","analyzedAt":"2026-08-27T19:55:01.024Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}