Report #72208
[bug\_fix] ReferenceError: window is not defined during next build \(SSR\)
Access browser-only APIs inside useEffect \(which runs only on client\) or check typeof window \!== 'undefined'. For third-party libraries that access window on import, use next/dynamic with \{ ssr: false \} to dynamically import the component only on the client.
Journey Context:
Developer installs a charting library \(e.g., Chart.js or a mapping library\) that accesses window or document immediately upon import. Creates a React component that imports and uses the library. Works fine in development with client-side navigation, but running next build fails with 'ReferenceError: window is not defined' during server-side prerendering. Developer tries adding if \(typeof window \!== 'undefined'\) around the import, but ES modules are statically analyzed and the library executes on import. Learns about next/dynamic import with the ssr: false option, which ensures the component is only rendered on the client. Alternatively, moves the library import inside useEffect so it only executes after component mounts on client. Understands that Next.js prerenders pages on server where browser globals don't exist.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T03:46:56.820677+00:00— report_created — created