Leaflet.markercluster: Does not work with ES6 imports for leaflet
It seems this plugin makes the assumption that L
is still exported globally.
However using leaflet the ES6 way with direct imports like
import { map } from "leaflet/src/map/Map"
does not expose a L
global anymore. So trying to include this plugins MarkerClusterGroup
fails cause of this definition.
export var MarkerClusterGroup = L.MarkerClusterGroup = L.FeatureGroup.extend({
The correct way would be to write this like this:
import { FeatureGroup } from "leaflet/src/layer/FeatureGroup"
export var MarkerClusterGroup = FeatureGroup.extend({
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 9
- Comments: 30 (6 by maintainers)
Commits related to this issue
- Fix markerclustering error Fixed through https://github.com/Leaflet/Leaflet.markercluster/issues/874#issuecomment-437895103 — committed to meine-stadt-transparent/meine-stadt-transparent by konstin 6 years ago
- Fix markerclustering error Fixed through https://github.com/Leaflet/Leaflet.markercluster/issues/874#issuecomment-437895103 — committed to meine-stadt-transparent/meine-stadt-transparent by konstin 6 years ago
Coming back here after 2 years I’d have thought this issue to be resolved. It seems its still not possible to properly import leaflet and this plugin. I am going to open a new PR over at leaflet to suggest a 2.0 release and intentionally breaking all the plugins depending on a global ‘L’ var. It seems otherwise we can not get any plugin owner to move forward to packages.
Finally found the solution
instead of using import * as L from ‘leaflet’
I use
import L from ‘leaflet’
some reading http://www.thedreaming.org/2017/04/28/es6-imports-babel/
just in case anybody runs into the same situation I had here: I tried to go from;
to:
I banged my head against a wall until I realized I had to call it with
new
:I guess this still has not been fixed?
I’ve made PR #984 that should fix this few days ago, I’d like some backup.
Basically in source files
L
is used as global and it leaked to the bundle. I’ve configured rollup to feedL
variable from UMD require - which works as well with globalL
outside the bundle.And it’s ok with guidelines: https://github.com/Leaflet/Leaflet/blob/master/PLUGIN-GUIDE.md#module-loaders
works fine for me if you just import all of it…
import 'leaflet/dist/leaflet.css' import 'leaflet/dist/leaflet' import 'leaflet-draw' import 'leaflet-draw/dist/leaflet.draw.css' import 'leaflet.markercluster' import 'leaflet.markercluster/dist/MarkerCluster.css'
@danzel What do you say about this? Shall we try to make this package independent on global
L
and instead use ES6 imports?I’m using
and
The following worked for me:
Right under the imports, I have used this to declare L.
const L = window[‘L’];
Make things working on
leaflet-src.esm.js
and withoutwindow.L
:Leaflet.markercluster-1.3.0
leaflet-1.3.2
(NOT 1.3.3 or 1.3.1), see leaflet#6239Solution based on earlier crunch (L<=1.3.1).
Yes but then you throw away all the benefits of ES6 modules. Which is to load only what you really need.
A guess the way to solve this is to stop depending on
window.L
in all the files, and instead do ES import, likeleaflet/leaflet
does. It would be a pretty great step forward.Same as above mentioned by @azrin1972 . Only difference in my setup is I’m using Webpack.
The
'L' is not defined no-undef
is coming from eslint. TheTypeError: L.markerClusterGroup is not a function
is a fatal error that stops MarkerCluster from working.Yeah, I’m interested in this proper solution as well…
I’ve taken from the above and was able to get it working, although, as mentioned without the benefit of ES6 and selectively loading what was needed.
Related PRs and discussions for Leaflet:
I’d love to figure out how to use the correct, minimalistic ES6 way with my custom Leaflet setup. The only plugins I’m using are markercluster, because who wouldn’t use it?! and the Google maps layer.
If I use:
import * as L from 'leaflet';
instead ofimport 'leaflet/dist/leaflet';
, I getTypeError: L.markerClusterGroup is not a function
.I was having a similar problem using leaflet in Vite:
No matching export in "node_modules/leaflet/dist/leaflet-src.esm.js" for import "toLatLngBounds"
Using
import 'leaflet/dist/leaflet';
insteadimport L from 'leaflet';
orimport 'leaflet';
worked for me.since we’re posting hotfixs. I’ve got a Vue app in Quasar 2. Before the Vue app is initialized:
in a component then: