Report #83312
[bug\_fix] window is not defined / document is not defined during Next.js build \(Server Component\)
Add the 'use client' directive at the top of the file to mark it as a Client Component, or gate the browser API access inside a useEffect that only runs after mount.
Journey Context:
Developer is migrating from Next.js Pages Router to App Router. They create a new file at \`app/page.tsx\` and paste code that worked in the old \`pages/index.tsx\`: \`const width = window.innerWidth;\`. Upon running \`next build\`, the build fails with 'ReferenceError: window is not defined'. Developer is confused because it worked before; they don't realize App Router defaults to Server Components which run in Node.js during SSR/SSG where \`window\` doesn't exist. They try to fix it by wrapping in \`if \(typeof window \!== 'undefined'\)\`, but this causes a hydration mismatch because the server renders \`null\` while the client renders the actual value. Finally, they discover the \`'use client'\` directive. Adding it to the top of the file tells Next.js to treat this as a Client Component, shipping the code to the browser where \`window\` exists, resolving the error without hydration issues.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T22:25:37.276056+00:00— report_created — created