Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Leaflet : Uncaught ReferenceError: L is not defined

Hello,

I'm searching for some hours now to find the reason why leaflet doesn't work.

It's an application with Laravel 10 and ViteJS.

Here is my app.js.

import './bootstrap';

import Alpine from 'alpinejs'

window.Alpine = Alpine;

Alpine.start();

import { L } from 'leaflet';
import { GeoSearchControl, OpenStreetMapProvider } from 'leaflet-geosearch';

window.addEventListener('DOMContentLoaded', event => {
    window.L = L;
	window.OpenStreetMapProvider = OpenStreetMapProvider;
	window.GeoSearchControl = GeoSearchControl;
});

And here the view.


<script>
	var latitude = '{{ $player->latitude }}';
	var longitude = '{{ $player->longitude }}';

	var map = L.map('map').setView([latitude, longitude], 6)

	L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
	    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
	}).addTo(map);

	var icon = new L.Icon.Default();
	icon.options.shadowSize = [0,0];

	L.marker([latitude, longitude], {icon: icon})
		.addTo(map)
		.bindPopup('{{ $player->localization_string }}')
		.openPopup();
</script>

And I get this error.

Uncaught ReferenceError: L is not defined

If you have any advice for me, it would be great.

Thanks a lot ;).

V

0 likes
2 replies
piljac1's avatar

You define your L property on the window after the DOMContentLoaded event has been fired. The code in your script tag is getting executed right away when you access your page (before DOMContentLoaded fires). Is there a reason that you assign your map related variables to your window in the DOMContentLoaded callback? Because it shouldn't be necessary.

1 like
vincent15000's avatar

@piljac1 Effectively you're right, I put the definition of the L property outside the DomContentLoaded, but this doesn't change anything, the problem is the same.

Do you have any other idea ?

1 like

Please or to participate in this conversation.