Next.js 15: ReferenceError: window is not defined
ReferenceError: window is not defined
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1103:32)
at Function.Module._load (node:internal/modules/cjs/loader:968:12)
at Module.require (node:internal/modules/cjs/loader:1281:19)
at require (node:internal/modules/cjs/helpers:136:16)
at Object.<anonymous> (/app/.next/server/app/page.js:1:100)
at __webpack_require__ (/app/.next/server/webpack-runtime.js:33:42)
Immediate Remediation
typescript
if (typeof window !== 'undefined') { /* your code */ }Root Cause Analysis
Next.js 15 runs both on the server (Node.js) and client (browser). Accessing `window` at module top-level or during SSR (Server Components) attempts to use a browser-only global that doesn't exist on the server, causing the ReferenceError.
Verification & Guardrails
- Ensure your component is a Client Component with 'use client' directive if it relies on browser APIs like `window`, `document`, or `localStorage`.
- For dynamic imports of browser-only libraries, use `next/dynamic` with `ssr: false` to skip server-side rendering.
- Verify that you haven't accidentally imported a module that accesses `window` at the top level in a Server Component or during static generation (e.g., in `getStaticProps`).
Have a custom or uncategorized crash?
Run your trace through our in-memory client privacy sandbox for instant SRE remediation.