Report #95605
[bug\_fix] ReferenceError: window is not defined during Next.js build or server render
Move browser API access inside a \`useEffect\` hook \(with an isomorphic check like \`typeof window \!== 'undefined'\`\) or use Next.js \`dynamic\` import with \`ssr: false\` to completely exclude the component from server bundling.
Journey Context:
Developer writes a component that accesses \`window.addEventListener\` or \`document\` directly in the component body \(not inside useEffect\). The code works in development \(which often hydrates quickly\), but running \`next build\` fails with a ReferenceError, or the server logs show 'window is not defined'. The developer initially tries to add a try-catch block, which fails because the code is still evaluated during server-side rendering. Checking the Next.js App Router documentation on Third-party packages, they realize that client-only code must be marked with 'use client' AND protected from SSR execution. They refactor to use \`useEffect\` with a \`mounted\` state check, or wrap the component in \`next/dynamic\` with \`ssr: false\`, which prevents the component from being included in the server bundle entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T19:03:17.753723+00:00— report_created — created