{"record":{"id":"0a248768eb655c46","repo":"sveltejs/kit","slug":"can-only-read-the-current-request-event-inside-fun","errorCode":null,"errorMessage":"Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, endpoints, and other server hooks.","messagePattern":"Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, endpoints, and other server hooks\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/kit/src/exports/internal/server/event.js","lineNumber":40,"sourceCode":" *\n * In environments without [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage), this must be called synchronously (i.e. not after an `await`).\n * @since 2.20.0\n *\n * @returns {RequestEvent}\n */\nexport function getRequestEvent() {\n\tconst event = try_get_request_store()?.event;\n\n\tif (!event) {\n\t\tlet message =\n\t\t\t'Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, endpoints, and other server hooks.';\n\n\t\tif (!als) {\n\t\t\tmessage +=\n\t\t\t\t' In environments without `AsyncLocalStorage`, the event must be read synchronously, not after an `await`.';\n\t\t}\n\n\t\tthrow new Error(message);\n\t}\n\n\treturn event;\n}\n\nexport function get_request_store() {\n\tconst result = try_get_request_store();\n\tif (!result) {\n\t\tlet message = 'Could not get the request store.';\n\n\t\tif (als) {\n\t\t\tmessage += ' This is an internal error.';\n\t\t} else {\n\t\t\tmessage +=\n\t\t\t\t' In environments without `AsyncLocalStorage`, the request store (used by e.g. remote functions) must be accessed synchronously, not after an `await`.' +\n\t\t\t\t' If it was accessed synchronously then this is an internal error.';\n\t\t}\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/exports/internal/server/event.js#L22-L58","documentation":"`getRequestEvent()` reads the current `RequestEvent` from `AsyncLocalStorage`. If no event is stored in the current async context, SvelteKit throws because there is no request in flight for this code. The message also warns that in non-ALS environments the event must be read synchronously before any `await`.","triggerScenarios":"Calling `getRequestEvent()` outside a request lifecycle — e.g. at module top level, inside `setTimeout`/event-emitter callbacks detached from the request, or after `await` in an environment without `AsyncLocalStorage`.","commonSituations":"Using `getRequestEvent()` in a shared module imported by both server and client; calling it in a `setInterval` or untracked promise callback; reading it in an endpoint after several awaits in a non-Node runtime (Cloudflare Workers).","solutions":["Call `getRequestEvent()` synchronously at the top of the function, capture the event, and use that reference after any `await`.","Only call it inside server request contexts: `load`, actions, endpoints, `handle` hook, remote functions.","Capture the event before the first `await` if targeting runtimes without `AsyncLocalStorage`.","Pass the event explicitly as a parameter to helper functions instead of pulling it from ambient context."],"exampleFix":"// before\nexport async function load() {\n  await db.connect();\n  const event = getRequestEvent(); // too late without ALS\n}\n// after\nexport async function load() {\n  const event = getRequestEvent();\n  await db.connect();\n  const { cookies } = event;\n}","handlingStrategy":"validation","validationCode":"// capture synchronously at entry of a server context\nlet event;\ntry { event = getRequestEvent(); } catch { event = null; }\nif (!event) throw new Error('getRequestEvent must be called within a server request context, synchronously');","typeGuard":null,"tryCatchPattern":"let event;\ntry {\n  event = getRequestEvent();\n} catch (e) {\n  // outside request context — use fallback/defaults\n  event = null;\n}\nif (event) { /* use event.cookies etc. */ }","preventionTips":["Call getRequestEvent() as the first line of load/actions/endpoints, before any await.","Never call it in module scope, timers, or detached callbacks.","Pass the event explicitly to helpers instead of ambient access.","On non-Node runtimes, avoid reading it after await boundaries."],"tags":["async-context","asynclocalstorage","request-event","sveltekit"],"backgroundTag":"missing-async-context","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}