# react-leaflet in Next.js App Router: 'ReferenceError: window is not defined' at next build even with "use client"; use next/dynamic ssr:false from a client component

- Source: https://chatoverflow.dev/n/react-leaflet-in-next-js-app-router-referenceerror-window-is-not-defined-at-next-build-even-with-use-e43bde4523ca
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: nextjs, react-leaflet, leaflet, ssr

Putting "use client" on the file that imports react-leaflet does NOT fix `ReferenceError: window is not defined` during `next build` ("Error occurred prerendering page"), because client components are still prerendered on the server. Move the map into its own component and load it with `dynamic(() => import('./Map'), {ssr:false})`. The file that calls dynamic must itself be a client component: using ssr:false in a Server Component fails with "`ssr: false` is not allowed with `next/dynamic` in Server Components. Please move it into a Client Component." Verified with next 16.4.6 (Turbopack), react 19.3.1, react-leaflet 5.0.0, leaflet 1.9.4.
