Report #15301
[bug\_fix] ReferenceError: window is not defined / document is not defined
Add "use client" directive to the file using browser APIs, or guard the code with typeof window \!== 'undefined' for isomorphic usage
Journey Context:
Developer writes a utility function that accesses window.localStorage at the top level of a module, or uses document.querySelector inside a component that isn't marked as a client component. When running next build, the build fails with "window is not defined". Developer tries wrapping in a try-catch, but the error persists because the code is evaluated during server-side rendering. They check the Next.js composition patterns documentation and realize that browser APIs are only available in Client Components. They either add "use client" to the specific component file, or refactor to check typeof window \!== 'undefined' before accessing browser APIs, allowing the code to safely execute in both environments.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:44:57.774648+00:00— report_created — created