Fix "ReferenceError: window is not defined" in Next.js & React SSR
Developer guide and instant terminal command to diagnose and solve this error.
Why Does This Error Happen?
Code accessing browser-only APIs (window, document, localStorage, navigator) was executed on the server during Server-Side Rendering (SSR) or Static Site Generation (SSG).
Instant Terminal One-Liner Fixes
Comprehensive Step-by-Step Resolution
- 1Guard window access with "typeof window !== 'undefined'".
- 2Move browser-dependent initialization inside a "useEffect(() => { ... }, [])" hook.
- 3In Next.js App Router, add "'use client';" at the very top of the file.
- 4Use dynamic import with ssr: false: "const DynamicComp = dynamic(() => import(...), { ssr: false })".
Troubleshoot using the TypeScript Interface Generator
Use our client-side utility with zero server upload for instant debugging.
Frequently Asked Questions
Frequently Asked Questions
Everything you need to know regarding specifications, syntax, and security best practices.
Code accessing browser-only APIs (window, document, localStorage, navigator) was executed on the server during Server-Side Rendering (SSR) or Static Site Generation (SSG).
Related Developer Errors
Fix "SyntaxError: Unexpected token < in JSON at position 0"
Fix "CORS Policy: No Access-Control-Allow-Origin header is present"
Fix "TypeError: Cannot read properties of undefined (reading 'xyz')"
Fix "npm ERR! code ERESOLVE unable to resolve dependency tree"