Report #47556
[bug\_fix] window is not defined / document is not defined during SSR or static generation
Move browser-only code inside useEffect \(which only runs on client\), or use dynamic import with ssr: false for libraries that access window at import time, or check typeof window \!== 'undefined' inside useEffect or event handlers \(not at module level\).
Journey Context:
Developer imports a third-party library \(e.g., charting library, maps, analytics\) that accesses window or document at the top level of the module. When running next build or refreshing the page, they get 'ReferenceError: window is not defined'. They try wrapping the component in a check for typeof window \!== 'undefined', but the error persists because the import itself happens before the component renders. They try using the library inside useEffect only, but the import statement at the top still executes. The solution involves either using next/dynamic with ssr: false to prevent the module from loading during SSR, or using a dynamic import\(\) inside useEffect to only load the library on the client side, ensuring window is only accessed in the browser environment.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T10:18:40.460756+00:00— report_created — created