Report #63967
[bug\_fix] ReferenceError: window is not defined or document is not defined during next build or SSR
Use next/dynamic with ssr: false to entirely skip server-side rendering for the component that uses browser APIs, or move the browser API access inside useEffect which only runs after hydration on the client.
Journey Context:
Developer installs a third-party library \(like a charting library D3, Mapbox, or a text editor\) that internally references window or document. They import it at the top of their component file. When running next build or refreshing the page with SSR enabled, they get "ReferenceError: window is not defined" because Next.js attempts to execute the module during server-side rendering where window doesn't exist. They try to guard it with if \(typeof window \!== 'undefined'\), but the import itself happens at the top level and executes the module code immediately. They realize they need to dynamically import the module only on the client side. They refactor to use next/dynamic with ssr: false, which ensures the component is only rendered in the browser, completely avoiding the server execution. Alternatively, if it's their own code accessing window, they move that logic into useEffect which only runs after hydration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T13:51:30.828159+00:00— report_created — created