React-Leaflet marker files not found

Posted on

React-Leaflet marker files not found – Even if we have a good project plan and a logical concept, we will spend the majority of our time correcting errors abaout javascript and leaflet. Furthermore, our application can run without obvious errors with JavaScript, we must use various ways to ensure that everything is operating properly. In general, there are two types of errors that you’ll encounter while doing something wrong in code: Syntax Errors and Logic Errors. To make bug fixing easier, every JavaScript error is captured with a full stack trace and the specific line of source code marked. To assist you in resolving the JavaScript error, look at the discuss below to fix problem about React-Leaflet marker files not found.

Problem :

I’ve got very simple code to display a map using react-leaflet and place a marker on it. However, i get the following two errors in my browser console

GET http://localhost:8080/marker-icon-2x.png 404 (Not Found)

GET http://localhost:8080/marker-shadow.png 404 (Not Found)

I tried to fix this issue by downloading those two images and placing them at the root. It works. However, how can i change the URL the react-leaflet marker element looks for the marker images? I’d like to store them in “./images” rather than at the root.

Solution :

Try to do this 🙂

React leaflet for some reason do not include images and you will need to reset
default icons image.

Below is some working example, I hope it will solve your issue.

You also can add icons from one of public link

import React, { Component } from 'react';
import L from 'leaflet';
import {
    Map, TileLayer, Marker, Popup
} from 'react-leaflet'
import 'leaflet/dist/leaflet.css';
import './style.css';

import icon from 'leaflet/dist/images/marker-icon.png';
import iconShadow from 'leaflet/dist/images/marker-shadow.png';

let DefaultIcon = L.icon({
    iconUrl: icon,
    shadowUrl: iconShadow

L.Marker.prototype.options.icon = DefaultIcon;

Here is the solution that worked for me:

I added the following lines in the top of the file:

import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
import 'leaflet/dist/leaflet.css';
import L from 'leaflet';

delete L.Icon.Default.prototype._getIconUrl;

    iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png').default,
    iconUrl: require('leaflet/dist/images/marker-icon.png').default,
    shadowUrl: require('leaflet/dist/images/marker-shadow.png').default

Adding answer for Next.js

  1. Copy over marker icon from node_modules/leaflet/dist/images to public/images something like /images/marker-icon.png

  2. Create Leaflet icon reference and use the reference in Marker

const icon = L.icon({ iconUrl: "/images/marker-icon.png" });

// some other code

<Marker key={} position={position} icon={icon}>

// rest of the code

It seems not all stuff is properly integrated together when using react, leaflet and react-leaflet. I had the same problem and found this

You need to setup leafelet itself again, as something brokes after importing leaflet.css.

Hope it helps

Copy all images from leaflet package to the public directory:

cp node_modules/leaflet/dist/images/* {PUBLIC_WEB_DIRECTORY}/leaflet_images/

Fix the path in Leaflet

import L from 'leaflet';

What ended up fixing this for me was removing:

import ‘leaflet/dist/leaflet.css’;

from the file in which my map components was in. I ended up importing leaflet css through the create-react-app index.html file and my marker was able to load alongside my map. Hope this helps anyone stuck.

Elaborated answer from @ch4nd4n’s answer and adapted for leaflet (v1.8.0) for reactjs (v17).

import iconMarker from 'leaflet/dist/images/marker-icon.png'
import iconRetina from 'leaflet/dist/images/marker-icon-2x.png'
import iconShadow from 'leaflet/dist/images/marker-shadow.png'


const icon = L.icon({ 
    iconUrl: iconMarker, 
    shadowUrl: iconShadow 

Add icon prop to the Marker component.

<Marker key={index} position={[, loc.long]} icon={icon}>
    <Popup><h3>{}</h3> {loc.address}</Popup>

Leave a Reply

Your email address will not be published. Required fields are marked *