Report #104310
[bug\_fix] ReferenceError: window is not defined
Wrap browser-only code in a \`useEffect\` hook, or check \`typeof window \!== 'undefined'\` before accessing \`window\`. For Next.js, also consider using dynamic imports with \`ssr: false\`.
Journey Context:
I was integrating a third-party charting library \(Chart.js\) in a Next.js page. I imported the library at the top of the component and called \`new Chart\(\)\` in the component body. The build succeeded, but when I loaded the page, I got 'ReferenceError: window is not defined'. The server-side render tried to execute the \`new Chart\(\)\` call, but \`window\` doesn't exist in Node.js. I moved the Chart.js initialization into a \`useEffect\` \(which only runs on the client\). That solved it. For libraries that require \`window\` at import time, I used dynamic import with \`\{ ssr: false \}\` via \`next/dynamic\`. The root cause: Next.js pre-renders components on the server, and any synchronous browser API access fails.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-26T20:07:08.219543+00:00— report_created — created