Leaflet map loads but the marker icon is broken or missing
Marker shows as a broken image iconShort 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.
Fix it fast
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 import2Match what you see, then jump to its fix
- 1
The map tiles are laid out wrongly and no Leaflet styles applyLeaflet's stylesheet is not loaded through the bundlerFix - 2
The 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
The marker request points at a path that does not exist in your buildThe marker has no working icon image of its ownFix
- 1
What does the error look like?
Marker shows as a broken image iconMap 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 serveConfirmed by leafletjs.com
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.js2Fix
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 distConfirmed by webpack.js.org
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 leaflet2Fix
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 serveConfirmed by leafletjs.com
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 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
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix