sveltejs/kit · error

Can only read the current request event inside functions inv

Error message

Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, endpoints, and other server hooks.

What it means

`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`.

Source

Thrown at packages/kit/src/exports/internal/server/event.js:40

 *
 * In environments without [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage), this must be called synchronously (i.e. not after an `await`).
 * @since 2.20.0
 *
 * @returns {RequestEvent}
 */
export function getRequestEvent() {
	const event = try_get_request_store()?.event;

	if (!event) {
		let message =
			'Can only read the current request event inside functions invoked during `handle`, such as server `load` functions, actions, endpoints, and other server hooks.';

		if (!als) {
			message +=
				' In environments without `AsyncLocalStorage`, the event must be read synchronously, not after an `await`.';
		}

		throw new Error(message);
	}

	return event;
}

export function get_request_store() {
	const result = try_get_request_store();
	if (!result) {
		let message = 'Could not get the request store.';

		if (als) {
			message += ' This is an internal error.';
		} else {
			message +=
				' In environments without `AsyncLocalStorage`, the request store (used by e.g. remote functions) must be accessed synchronously, not after an `await`.' +
				' If it was accessed synchronously then this is an internal error.';
		}

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Call `getRequestEvent()` synchronously at the top of the function, capture the event, and use that reference after any `await`.
  2. Only call it inside server request contexts: `load`, actions, endpoints, `handle` hook, remote functions.
  3. Capture the event before the first `await` if targeting runtimes without `AsyncLocalStorage`.
  4. Pass the event explicitly as a parameter to helper functions instead of pulling it from ambient context.

Example fix

// before
export async function load() {
  await db.connect();
  const event = getRequestEvent(); // too late without ALS
}
// after
export async function load() {
  const event = getRequestEvent();
  await db.connect();
  const { cookies } = event;
}
Defensive patterns

Strategy: validation

Validate before calling

// capture synchronously at entry of a server context
let event;
try { event = getRequestEvent(); } catch { event = null; }
if (!event) throw new Error('getRequestEvent must be called within a server request context, synchronously');

Try / catch

let event;
try {
  event = getRequestEvent();
} catch (e) {
  // outside request context — use fallback/defaults
  event = null;
}
if (event) { /* use event.cookies etc. */ }

Prevention

When it happens

Trigger: 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`.

Common situations: 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).

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/0a248768eb655c46. Report an issue: GitHub.