Report #5641
[bug\_fix] ReferenceError: window is not defined or document is not defined
Use Next.js dynamic import with ssr: false to ensure the library only loads on the client, or check typeof window \!== 'undefined' inside useEffect. Root cause: Next.js runs code during server-side rendering \(Node.js\) where browser globals like window and document do not exist; referencing them at import time or during server render throws.
Journey Context:
Developer installs a charting library like Chart.js or a mapping library like Mapbox that assumes a browser environment. They import it at the top of their React component. Running npm run build immediately fails with 'window is not defined'. They try guarding with if \(typeof window \!== 'undefined'\) around the import, but the import statement itself is evaluated at parse time. They attempt to move the import inside useEffect but struggle with async imports. Finally, they find next/dynamic and configure it with ssr: false, which completely excludes the library from the server bundle and resolves the error.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T21:48:03.416113+00:00— report_created — created