Report #61711
[bug\_fix] ReferenceError: window is not defined during next build or SSR
Check \`typeof window \!== 'undefined'\` before accessing browser globals, or use \`next/dynamic\` with \`ssr: false\` to exclude the component from server rendering entirely.
Journey Context:
A developer installs a charting library like Chart.js that internally references \`window\` for canvas rendering. They import and use the component in a Next.js page. When running \`next build\`, the build crashes with "ReferenceError: window is not defined". The developer realizes this happens because Next.js pre-renders pages on the server \(Node.js\), where \`window\` doesn't exist. They try to guard the usage with \`if \(typeof window \!== 'undefined'\)\`, but the import statement itself at the top of the file executes the library's module code, which touches \`window\` immediately. They then try to move the import inside \`useEffect\`, which works because effects run client-side, but dynamic imports are cleaner. Finally, they use \`next/dynamic\` to import the Chart component with \`ssr: false\`. This tells Next.js to skip server-rendering that component entirely, injecting it only in the browser bundle. The build succeeds because the server never executes the \`window\`-touching code.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T10:04:09.734170+00:00— report_created — created