remix-run/remix · error · Error

No router found in request context.

Error message

No router found in request context.

What it means

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.

Source

Thrown at packages/fetch-router/src/lib/request-context.ts:344

      )
    }

    this.#contextProperties.set(property, key)
    this.#contextPropertyKeys.set(key, property)

    Object.defineProperty(this, property, {
      get: () => this.get(key),
    })
  }

  #router?: Router<any>

  /**
   * The router handling this request.
   */
  get router(): Router<RequestContext<any, entries>> {
    if (this.#router == null) {
      throw new Error('No router found in request context.')
    }

    return this.#router as Router<RequestContext<any, entries>>
  }

  set router(router: Router<any>) {
    this.#router = router
  }

  /**
   * The URL of the current request.
   */
  url: URL
}

export interface RequestContext<
  params extends Record<string, any> = {},
  entries extends ContextEntries = [],

View on GitHub (pinned to 9696913134)

Solutions

  1. In tests, construct the context through the router's test helpers or stub a router
  2. Check 'router' in ctx / optional access before use if the API supports it
  3. Restructure code so router access happens only inside route handlers

Example fix

// before
const ctx = new RequestContext(request)
ctx.router // throws
// after
const router = createRouter(routes)
const ctx = router.createContext?.(request) ?? new RequestContext(request, { router })
ctx.router // ok
Defensive patterns

Strategy: try-catch

Validate before calling

// in tests, build context through the router or pass one
const ctx = new RequestContext(request, { router })

Type guard

const hasRouter = (ctx: RequestContext): boolean => (ctx as any).#router != null

Try / catch

let router: Router | undefined
try { router = ctx.router } catch { router = undefined }

Prevention

When it happens

Trigger: Instantiating RequestContext manually (e.g. new RequestContext(request)) in tests and then accessing ctx.router; accessing router in code detached from the routing pipeline.

Common situations: Unit-testing middleware or route handlers with a hand-built context; helpers reused in scripts/tests where no router exists.

Related errors


AI-assisted analysis of remix-run/remix@9696913134 (2026-08-27). Data as JSON: /api/errors/4ea97a9b0b7d3a36. Report an issue: GitHub.