Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Leaflet cannot find the map container element

ErrorError: 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.

  • Applies to Leaflet 1.9, React Leaflet 5, React 19, Vite 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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'));
  2. 2Match what you see, then jump to its fix

    1. 1The log prints null, and the script tag sits in head or above the divThe script runs before the div existsFix
    2. 2The log prints null even though a map div is on the pageThe id in L.map does not match the divFix
    3. 3The code calls L.map(ref.current) or document.getElementById in the component bodyIn React, the ref is read during renderFix

What does the error look like?

  • Error: Map container not found.
  • L.map('map') throws on page load
  • The 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.html

2Fix

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

Did this fix it?

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.html

2Fix

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.