solidjs/solid · error · Error
Attempting to use server context in non-server build
Error message
Attempting to use server context in non-server build
What it means
provideRequestEvent uses node:async_hooks AsyncLocalStorage to expose the request event during SSR, so it only works when isServer is true (server conditions resolved by the bundler). Importing or calling it in a client bundle throws immediately to flag the environment mismatch.
Source
Thrown at packages/solid/web/storage/src/index.ts:7
import { AsyncLocalStorage } from "node:async_hooks";
import type { RequestEvent } from "solid-js/web";
import { isServer, RequestContext } from "solid-js/web";
// using global on a symbol for locating it later and detaching for environments that don't support it.
export function provideRequestEvent<T extends RequestEvent, U>(init: T, cb: () => U): U {
if (!isServer) throw new Error("Attempting to use server context in non-server build");
const ctx: AsyncLocalStorage<T> = ((globalThis as any)[RequestContext] =
(globalThis as any)[RequestContext] || new AsyncLocalStorage<T>());
return ctx.run(init, cb);
}
View on GitHub (pinned to f47845f9cc)
Solutions
- Move provideRequestEvent usage into server-only modules and keep them out of client entry graphs
- Guard with isServer: if (isServer) provideRequestEvent(...) else fallback
- Add 'solid' server conditions (e.g. via vite-plugin-solid conditions: ['node', 'solid']) so server builds resolve solid-js/web to server code
Example fix
// before (shared util imported by client)
export function handler(req, cb) {
return provideRequestEvent(req, cb); // throws in browser build
}
// after
import { isServer } from 'solid-js/web';
export function handler(req, cb) {
return isServer
? provideRequestEvent(req, cb)
: cb();
} Defensive patterns
Strategy: validation
Validate before calling
import { isServer } from 'solid-js/web';
export const runWithReq = isServer
? (req: RequestEvent, cb: () => any) => provideRequestEvent(req, cb)
: (_req: unknown, cb: () => any) => cb(); Type guard
import { isServer } from 'solid-js/web';
const canProvideRequestEvent = (): boolean => isServer; Try / catch
try { return provideRequestEvent(req, cb); } catch (e) { if (/server context in non-server/.test(String(e))) return cb(); throw e; } Prevention
- Keep server-only imports out of client entry graphs (split entry points)
- Guard with isServer before calling server utilities
- Configure bundler conditions so server builds resolve solid server code
When it happens
Trigger: Calling provideRequestEvent(req, cb) in code bundled for the browser; server utilities imported eagerly into shared/isomorphic modules; using solid-js/web/storage helpers (useRequestEvent outside a provided run) in a client entry.
Common situations: Bundler 'solid' export condition resolving to browser because the file was imported from client code; testing server middleware in a browser-targeted vitest config; pulling a server-only helper into a shared util file.
Related errors
- Expected the observer to be an object.
- getContextId cannot be used under non-hydrating context
- getNextContextId cannot be used under non-hydrating context
- You appear to have multiple instances of Solid. This can lea
- Expected the observer to be an object.
AI-assisted analysis of solidjs/solid@f47845f9cc (2026-08-27).
Data as JSON: /api/errors/c5b6e045060f628f.
Report an issue: GitHub.