-1

I am developing a web application using React and different other libraries.

I am using Windy API (weather forecast) which uses leaflet library 1.4.0.

I would also like to use leaflet in the same application but this time using the latest version 1.7.1.

I import leaflet 1.4.0 with the CDN as described in the Windy API documentation.

I import the latest version with npm.

The problem is leaflet is creating a global variable L, so when I import the latest version of leaflet, the API windy is not working.

I tried to import leaflet with allias but it doesn't change the problem of the global variable.

Is possible to change the name of the global variable? Any suggestions to solve this problem?

Thank you !

1 Answer 1

0

You can use L.noConflict() which applies the old object to L and returns the Leaflet instance:

console.log(L.version) // 1.4.0
import 'leaflet';
console.log(L.version) // 1.7.1

var leaflet1_7 = L.noConflict();

console.log(L.version) // 1.4.0
console.log(leaflet1_7 ) // 1.7.1

But it can be that noConflict is not working properly, there where some bugs with it. But they are fixed in 1.8.0-beta.3 and in the coming release in the next days.

1
  • I use your solution in my project and it works well, thank you! But now, i want to use plugins to extend the leaflet object "Leaflet1_7" in your code (version1.8 now). I created a new question for this here : extend-leaflet-with-plugin-on-specific-leaflet-version. Do you have any idea how to manage this situation. Thank you
    – Fred
    Commented May 2, 2022 at 13:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.