Leaflet cannot find the map container element
Error: Map container not found.Short answer
Leaflet looks up the container by id or takes an element you pass. If the div is not in the page yet, the id is different, or in React you read a ref during render when it is still null, the lookup fails. Create the map after the div exists, use the same id, and in React use MapContainer or an effect.
Fix it fast
1Find out which cause you have
Ask the page whether the element exists at the moment the map is created. Run this in the console, or log it right before L.map.
console.log(document.getElementById('map'));2Match what you see, then jump to its fix
- 1
The log prints null, and the script tag sits in head or above the divThe script runs before the div existsFix - 2
The log prints null even though a map div is on the pageThe id in L.map does not match the divFix - 3
The code calls L.map(ref.current) or document.getElementById in the component bodyIn React, the ref is read during renderFix
- 1
What does the error look like?
Error: Map container not found.L.map('map') throws on page loadThe map works after a hot reload but not on a fresh load
Cause 1 of 3
The script runs before the div exists
1Check
The map goes in a div and Leaflet is given that div or its id. The quick start relies on module scripts, which run after the page is parsed, so the div already exists.
grep -n 'id="map"\|<script' index.html2Fix
Load the script as a module, put it after the div, or create the map after the DOM is ready.
<div id="map" style="height: 400px"></div>
<script type="module" src="/src/map.js"></script>3Verify
Reload and check that the console log now prints the element.
console.log(document.getElementById('map'));Confirmed by leafletjs.com
Cause 2 of 3
The id in L.map does not match the div
1Check
The Map constructor takes the id of a div, as a string, or the element itself. A different spelling or case gives no element.
grep -rn 'L.map(' src/ index.html2Fix
Use the same id in both places, or pass the element so no lookup by name happens.
const el = document.getElementById('map');
const map = L.map(el).setView([51.505, -0.09], 13);3Verify
The map renders after a fresh load.
console.log(map.getCenter());Confirmed by leafletjs.com
Cause 3 of 3
In React, the ref is read during render
1Check
During the first render the DOM nodes have not been created, so ref.current is null. React sets it during the commit, after the DOM is updated.
grep -rn 'ref.current\|getElementById' src/components/2Fix
Create the map in an effect, which runs after the commit, or use React Leaflet's MapContainer so you do not touch the DOM yourself.
import { MapContainer, TileLayer } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';
export default function Map() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: 400 }}>
<TileLayer url="https://tile.openstreetmap.org/{z}/{x}/{y}.png" />
</MapContainer>
);
}3Verify
The map shows tiles on a fresh load, not only after a hot reload.
console.log(document.querySelector('.leaflet-container'));Confirmed by react.dev
How do I stop it coming back?
- Give the map div an explicit height; the Leaflet quick start says it needs one.
- Load Leaflet's CSS, as the React Leaflet setup page says to.
- Treat MapContainer props as fixed after creation; only its children change.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix