Report #97733
[bug\_fix] ReferenceError: window is not defined during Next.js build
Guard browser-only globals with typeof window \!== 'undefined'. For code that must only run in the browser, import the component with next/dynamic and ssr: false, or read the value inside useEffect. Do not access document/window/localStorage at the top level of a module imported by a server component.
Journey Context:
I added a charting library that accessed window.innerWidth at import time to pick a default canvas size. Next.js build crashed with 'ReferenceError: window is not defined' because Node.js rendered the page before the client ever existed. I first tried typeof window in the component, but the crash happened during the import itself. Wrapping the chart in next/dynamic\(\{ ssr: false \}\) kept it out of the server bundle entirely. For simpler cases like reading localStorage for a theme, I now only access it inside useEffect or behind a typeof window guard.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-26T04:36:50.763194+00:00— report_created — created