Nitro · NITRO0035

Keep request data out of module-scope state

Report module-scope variables, objects, and Maps that Nitro server code fills with request data.
NITRO0035vite-doctor/nitrostatewarnSuggestion

Run this rule

pnpm vite-doctor . --framework nitro --rules nitro/state/no-request-data-in-module-state

Why it matters

Nitro imports each server module once per process. A lazy route handler is imported on its first request and h3 keeps the resolved handler, so every later request calls into the same module. Only the event, and the event.context object h3 creates with it, belong to one request. A module-scope let, an object written under a fixed key, or a Map set under a constant key that a handler fills from the event, its query, body, headers, cookies, or session is shared by every concurrent request, so one user's value can end up in another user's response. Doctor reports writes from h3 request utilities and from the event a handler receives. It skips stores keyed per request, such as a Map keyed by the route parameter, values whose origin the file does not show, and files outside a Nitro server directory. For Object.assign, Doctor skips slots when it cannot establish what a local source object contains at copy time; a shallow spread does not protect nested values from mutation.

Keep per-request values in local variables, or on event.context when middleware hands them to the route. A module-scope cache is fine when its key covers everything the value depends on; for upstream responses use defineCachedFunction with a getKey that includes the user or tenant.

Example

Keep request data on the event

Before

let currentUser: string | undefined

export default defineEventHandler((event) => {
  currentUser = getCookie(event, 'user-id')
  return getOrders(currentUser)
})

After

export default defineEventHandler((event) => {
  const userId = getCookie(event, 'user-id')
  event.context.userId = userId
  return getOrders(userId)
})
Copyright © 2026