Report #7259
[bug\_fix] ReferenceError: window is not defined during next build
Use \`next/dynamic\` with \`ssr: false\` to dynamically import the component that accesses browser APIs only on the client, or guard browser API access with \`if \(typeof window \!== 'undefined'\)\` checks inside \`useEffect\` \(which only runs client-side\).
Journey Context:
Developer runs \`npm run build\` for production deployment. The build fails with 'ReferenceError: window is not defined' pointing to a library or component that accesses \`window\` or \`document\` at the top level. The code works fine in \`npm run dev\` because Next.js might be client-side navigating, but during build, Next.js pre-renders pages in a Node.js environment where browser globals don't exist. Developer tries adding a check \`if \(window\)\` but it still fails because the import itself executes the code. They learn about \`next/dynamic\` and wrap the component import with \`dynamic\(\(\) => import\('../components/MyComponent'\), \{ ssr: false \}\)\`, understanding this prevents the component from being imported during server rendering, thus avoiding the server-side execution of browser-specific code.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:14:22.608297+00:00— report_created — created