Registry / web-framework / react-leaflet

react-leaflet

JSON →
library5.0.0jsnpmunverified

React Leaflet provides React components that wrap the Leaflet map library, offering a declarative way to integrate interactive maps into React applications. It abstracts Leaflet's imperative API into a set of composable React components, making it easier to manage map state and layers. The current stable version is 5.0.0, which requires React v19 and Leaflet v1.9.0 as peer dependencies. The library generally follows the major version releases of React and Leaflet, introducing breaking changes when these core dependencies update significantly. Its primary differentiator is providing a idiomatic React experience for Leaflet, avoiding direct DOM manipulation often associated with Leaflet's vanilla JavaScript API, and offering a robust, type-safe (TypeScript-friendly) development experience for geospatial applications within the React ecosystem.

npm install react-leaflet
INSTALL
IMPORT
SIG · REACT-LEAFLET
R
react-leaflet
web-frameworkjavascriptv5.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MapContainer
import { MapContainer } from 'react-leaflet'
const MapContainer = require('react-leaflet').MapContainer
Primary map component. CommonJS `require` is not supported since v4.0.0. Always use named imports.
TileLayer
import { TileLayer } from 'react-leaflet'
import TileLayer from 'react-leaflet/TileLayer'
Component for adding tile layers. All core components are named exports from the main package entry point.
Marker
import { Marker, Popup } from 'react-leaflet'
import { Marker } from 'react-leaflet/lib/Marker'
For adding markers and popups. Individual component paths are not exported and should not be used.

This quickstart demonstrates how to set up a basic Leaflet map in a React component using `react-leaflet`, including a `TileLayer` and a `Marker` with a `Popup`. It also addresses a common issue with missing default marker icons.

import React from 'react'; import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet'; import 'leaflet/dist/leaflet.css'; // Don't forget Leaflet's CSS! // Fix for default marker icon issue with Webpack/Vite import L from 'leaflet'; delete (L.Icon.Default.prototype as any)._getIconUrl; L.Icon.Default.mergeOptions({ iconRetinaUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon-2x.png', iconUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon.png', shadowUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-shadow.png', }); function MyMapComponent() { const position: [number, number] = [51.505, -0.09]; return ( <MapContainer center={position} zoom={13} scrollWheelZoom={false} style={{ height: '500px', width: '100%' }}> <TileLayer attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> <Marker position={position}> <Popup> A pretty CSS3 popup. <br /> Easily customizable. </Popup> </Marker> </MapContainer> ); } export default MyMapComponent;
Debug
Known issues
breakingReact v19 is now a required peer dependency. Applications must upgrade React to version 19.0.0 or later to use `react-leaflet` v5.
fix
Update your `react` and `react-dom` packages to `^19.0.0` in your `package.json` and reinstall dependencies.
affects: >=5.0.0
breakingThe `LeafletProvider` component has been removed from the core package in v5.0.0.
fix
Review your code for `LeafletProvider` usage. It's likely you no longer need it, as context is typically managed internally by `MapContainer`.
affects: >=5.0.0
breakingCommonJS (CJS) and UMD distributions have been removed since v4.0.0. The library is now exclusively ESM (ES Modules).
fix
Ensure your project is configured for ESM. Replace all `require('react-leaflet')` statements with `import { ... } from 'react-leaflet'`.
affects: >=4.0.0
breakingThe `useMapElement` hook, `MapConsumer` component, and `whenCreated` prop on `MapContainer` were removed in v4.0.0. `onOpen` and `onClose` props for `Popup` and `Tooltip` were also removed.
fix
For `whenCreated`, use a `ref` callback on `MapContainer` instead. For `useMapElement` and `MapConsumer`, use the `useMap` or `useMapEvents` hooks. For `Popup` and `Tooltip` events, rely on Leaflet's event system directly.
affects: >=4.0.0
gotchaLeaflet's default marker icons often appear broken in React applications due to Webpack/Vite not correctly locating the image assets. This is a common setup issue, not a `react-leaflet` bug.
fix
Explicitly set the `iconRetinaUrl`, `iconUrl`, and `shadowUrl` for `L.Icon.Default.prototype` before rendering any markers, or configure your bundler to handle static assets correctly. See the Quickstart example for a common workaround.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Invariant Violation: Minified React error #321; visit https://reactjs.org/docs/error-decoder.html?invariant=321 for the full message or use the non-minified dev environment for full errors.
Mismatch between `react` peer dependency version required by `react-leaflet` and the version installed in your project.
fix
Upgrade `react` and `react-dom` to `^19.0.0` (for `react-leaflet` v5) or `^18.0.0` (for `react-leaflet` v4).
TypeError: Cannot read properties of undefined (reading 'Icon') or Marker images are not displayed.
Leaflet's default marker images are not being found by the module bundler, leading to broken icon paths.
fix
Import Leaflet explicitly and then use `L.Icon.Default.mergeOptions` to provide direct URLs for the marker assets (e.g., from unpkg.com) before any `Marker` components are rendered. Also ensure `import 'leaflet/dist/leaflet.css';` is included.
ReferenceError: require is not defined in ES module scope
`react-leaflet` versions 4.0.0 and above are pure ES Modules (ESM) and no longer support CommonJS `require()` syntax.
fix
Change all `const { MapContainer } = require('react-leaflet');` statements to `import { MapContainer } from 'react-leaflet';` and ensure your project is configured for ESM.
Upgrade
Version history
5.0.0latest on npm
Audit
Dependencies
leafletrequiredCore mapping library functionality; required peer dependency for `react-leaflet` to operate.
reactrequiredCore React library for UI components; required peer dependency for `react-leaflet` to integrate.
react-domrequiredReact's DOM rendering engine; required peer dependency for `react-leaflet` to render map components.
Agent activity
6 hits · last 30 days
node
6
Resources
react-leaflet — npm install react-leaflet · libregistry