Maaz Zindani

Loading the index…

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

Leaflet map loads but the marker icon is broken or missing

ErrorMarker shows as a broken image icon

Short answer

In a webpack app the map tiles can load while the marker is a broken image. The usual reasons are that Leaflet's CSS is not imported, that webpack has no rule for the png files, or that the default icon URL cannot be found. Import the CSS, add an asset rule, and set the icon explicitly.

  • Applies to webpack 5, Leaflet 1.9, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the Network tab, filter by Img and look at the marker request. A 404 or a missing request tells you which cause applies.

    grep -rn "leaflet" src/ | grep import
  2. 2Match what you see, then jump to its fix

    1. 1The map tiles are laid out wrongly and no Leaflet styles applyLeaflet's stylesheet is not loaded through the bundlerFix
    2. 2The image request in the Network tab fails, or an imported png is not in the output folderwebpack has no rule that handles the png filesFix
    3. 3The marker request points at a path that does not exist in your buildThe marker has no working icon image of its ownFix

What does the error look like?

  • Marker shows as a broken image icon
  • Map tiles load but markers are invisible

Cause 1 of 3

Leaflet's stylesheet is not loaded through the bundler

1Check

With a bundler you import Leaflet's classes and its CSS yourself. Search your entry file for the CSS import.

grep -rn "leaflet.css" src/

2Fix

Import the stylesheet once, next to the Leaflet import, and make sure a CSS rule exists in the webpack config.

import L from 'leaflet';
import 'leaflet/dist/leaflet.css';

3Verify

Reload and check that the tile layout is correct and the marker has a size.

npx webpack serve

Confirmed by leafletjs.com

Did this fix it?

Cause 2 of 3

webpack has no rule that handles the png files

1Check

webpack 5 handles images through asset modules. Without a rule of type asset/resource (or asset), an imported png is not emitted.

grep -n "asset" webpack.config.js

2Fix

Add an asset module rule for images. It replaces file-loader and url-loader in webpack 5.

// webpack.config.js
module: {
  rules: [
    { test: /\.(png|jpe?g|gif|svg)$/i, type: 'asset/resource' },
  ],
},

3Verify

Rebuild and confirm the png files appear in the output folder.

npx webpack
ls dist

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

The marker has no working icon image of its own

1Check

A Leaflet Icon needs an iconUrl, the URL of the image. Leaflet's newest reference says the default marker is an inlined SVG, so this mostly affects older 1.x versions. Check which version you run.

npm ls leaflet

2Fix

Import your own marker image so webpack emits it, build an Icon from the imported URL and pass it to the marker. iconAnchor sets which point of the image sits on the map position.

import L from 'leaflet';
import markerPng from './assets/marker.png';

const icon = L.icon({
  iconUrl: markerPng,
  iconSize: [25, 41],
  iconAnchor: [12, 41],
});
L.marker([51.5, -0.09], { icon }).addTo(map);

3Verify

Reload the page; the marker should now show your image.

npx webpack serve

Confirmed by leafletjs.com

Did this fix it?

How do I stop it coming back?

  • Import Leaflet's CSS in the same file that creates the map.
  • Keep one asset rule for images instead of per-library workarounds.
  • Set the marker icon explicitly in a shared helper so every map uses the same one.

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

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

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