Report #80558
[bug\_fix] window is not defined during Next.js SSR/SSG build \(ReferenceError\)
Use Next.js dynamic import with \`ssr: false\` option to load the component/library only on the client, or ensure window access is only inside useEffect \(which doesn't run during SSR\).
Journey Context:
Developer integrates a browser-only library \(e.g., PDF viewer, maps, or charts like Chart.js\) into a Next.js application. They install the package and import it at the top of their component file: \`import \{ Chart \} from 'chart.js/auto'\`. When running \`npm run build\` for static generation or server-side rendering, the build fails with "ReferenceError: window is not defined" pointing into the node\_modules package. The developer tries adding a runtime check \`if \(typeof window \!== 'undefined'\)\` around the import, but the error persists because ES Module imports are evaluated at load time during the SSR phase, before runtime code runs. The developer discovers Next.js dynamic imports: \`import dynamic from 'next/dynamic'\` and then \`const ChartComponent = dynamic\(\(\) => import\('../components/MyChart'\), \{ ssr: false \}\)\`. This ensures the component code \(and its window-dependent imports\) are only loaded and executed in the browser, completely excluding it from the server bundle.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T17:49:02.261661+00:00— report_created — created