JavaScript & Node Error

Fix "ReferenceError: window is not defined" in Next.js & React SSR

Developer guide and instant terminal command to diagnose and solve this error.

Terminal Output
$ReferenceError: window is not defined (or document is not defined)

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

Linux (Bash / Zsh)
// Add at top of file: 'use client';
macOS (Terminal / iTerm2)
// Add at top of file: 'use client';
Windows (PowerShell Run as Administrator)
// Add at top of file: 'use client';

Comprehensive Step-by-Step Resolution

  1. 1Guard window access with "typeof window !== 'undefined'".
  2. 2Move browser-dependent initialization inside a "useEffect(() => { ... }, [])" hook.
  3. 3In Next.js App Router, add "'use client';" at the very top of the file.
  4. 4Use dynamic import with ssr: false: "const DynamicComp = dynamic(() => import(...), { ssr: false })".
Dedicated Companion Tool

Troubleshoot using the TypeScript Interface Generator

Use our client-side utility with zero server upload for instant debugging.

Open Tool

Frequently Asked Questions

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