Report #57026
[bug\_fix] \`window is not defined\` or \`document is not defined\` during SSR/build
Move the browser-specific code inside a \`useEffect\` hook \(which only runs on the client\) or check \`typeof window \!== 'undefined'\` before accessing the object. For initialization logic, use a state variable that updates in useEffect. Root cause: Next.js pre-renders pages on the server \(Node.js\) during build or SSR, where \`window\` and \`document\` globals do not exist.
Journey Context:
Developer writes a component that accesses \`window.localStorage.getItem\('theme'\)\` at the top level of the component function to set initial state. When running \`next build\`, the build fails with "ReferenceError: window is not defined". They try to guard with \`if \(window\)\` but that throws at parse time. They realize the code runs on the server during SSR. They refactor to use \`useState\(null\)\` initially and move the localStorage access into \`useEffect\(\(\) => \{ setTheme\(localStorage.getItem\('theme'\)\) \}, \[\]\)\` which only executes in the browser. The build succeeds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T02:12:31.854134+00:00— report_created — created