Report #73399
[bug\_fix] window is not defined during Next.js SSR/build
Move browser-only code inside \`useEffect\` \(which only runs after hydration on the client\), or use Next.js \`dynamic\` import with \`ssr: false\`, or guard with \`typeof window \!== 'undefined'\`. Root cause: Next.js pre-renders pages on the server \(Node.js\) where browser globals like \`window\`, \`document\`, and \`navigator\` do not exist.
Journey Context:
You install a third-party analytics library and initialize it at the top level of a component: \`window.analytics = new Analytics\(\)\`. In dev mode \(\`next dev\`\), it works fine because Next.js might be doing client-side navigation. But when you run \`npm run build\` for static generation, the build crashes with "ReferenceError: window is not defined". You try wrapping it in an if-check \`if \(window\)\`, but that throws during SSR because the parser hits the code before the check. You try moving the code inside a \`useEffect\(\(\) => \{ window.analytics = ... \}, \[\]\)\`, and the build passes because \`useEffect\` never runs during server render. Alternatively, you refactor to use \`dynamic\(\(\) => import\('analytics-js'\), \{ ssr: false \}\)\` which ensures the library is never loaded or executed on the server, eliminating the error entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T05:47:37.100234+00:00— report_created — created