Next.js: ReferenceError: window is not defined (server-side rendering)
Code touching the browser's window ran on the server during SSR/build, where it doesn't exist.
ReferenceError: window is not definedwindow is not defined next.jsReferenceError: self is not definedReferenceError: navigator is not definedError occurred prerendering page ReferenceError: window is not defined Meaning
Guard with typeof window !== 'undefined', move the access into useEffect (client only), or load the browser-only component with next/dynamic and ssr: false. Third-party libs that read window at import time need dynamic import.
Common causes
window.innerWidth/localStorageat module or render time- Library accessing window on import (charts, editors, maps)
- Server Component using browser API
- Top-level
windowaccess in utility files - Pre-rendering at build time
⚡ Quick fix
useEffect(() => { /* window usage */ }, [])const Map = dynamic(() => import('./Map'), { ssr: false })in a Client Component- Guard:
typeof window !== "undefined"
Detailed fix by platform
Next.js
"use client"; const Chart = dynamic(() => import("react-apexcharts"), { ssr: false });
How to diagnose
- Where — Render or import time?
- Library — Browser-only?
- Component — Client or server?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- [Vue warn]: Hydration node/text/children mismatch Vue: [Vue warn]: Hydration text content mismatch / Hydration node mismatch: server rendered element contains more child nodes than client vdom
- Can't resolve 'fs' Module not found: Can't resolve 'fs' (Next.js / webpack 5)
- Conflicting app and page file was found Next.js: ⨯ Conflicting app and page file was found, please remove the conflicting files to continue: "pages/index.tsx" - "app/page.tsx"
Most viewed in Next.js
- NEXT_REDIRECT Next.js: Error: NEXT_REDIRECT (redirect() inside try/catch)
- server-only Next.js: You're importing a component that needs "server-only". That only works in a Server Component
- cookies() should be awaited Next.js 15: Route "/x" used `cookies().get('token')`. `cookies()` should be awaited before using its value
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026