Report #94425
[bug\_fix] ReferenceError: window is not defined during Next.js SSR/SSG Build
Access \`window\` or \`document\` only inside \`useEffect\` \(which runs only on client\), or use \`next/dynamic\` with the \`ssr: false\` option to dynamically import the component that requires browser APIs.
Journey Context:
Developer installs a third-party charting library like \`chart.js\` or \`recharts\` that internally references \`window\` immediately upon import. In development \(\`next dev\`\), the app works because Next.js often falls back to client-side rendering. However, running \`next build\` fails with "ReferenceError: window is not defined". The developer realizes Node.js build environment has no \`window\`. They attempt to fix by checking \`typeof window \!== 'undefined'\`, but this is unreliable during React's render phase in SSR. The correct solution is to import the component using \`next/dynamic\` with \`ssr: false\`, ensuring the library is only bundled and executed in the browser, bypassing the Node.js build step entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T17:04:40.167184+00:00— report_created — created