Report #13921
[bug\_fix] window is not defined / document is not defined during Next.js build
Add the 'use client' directive at the top of the file to mark it as a Client Component, or guard browser API access with typeof window \!== 'undefined' checks inside useEffect.
Journey Context:
Developer imports a third-party charting library that accesses window.innerWidth immediately on import. In development \(next dev\), everything works. When running next build, the build fails with 'ReferenceError: window is not defined'. They realize Next.js App Router defaults to Server Components which execute in a Node.js environment without browser globals. They try conditional rendering with if \(typeof window \!== 'undefined'\) but the error persists because the import itself executes the code. They discover that adding 'use client' at the very top of the file \(before imports\) forces Next.js to treat the module as a Client Component, bundling it separately and executing it only in the browser. For third-party libraries that can't be modified, they use dynamic imports with ssr: false inside useEffect as an alternative.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:13:15.516386+00:00— report_created — created