Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript] import { Map } from maplibre-gl gives error 'cannot find Map' in browser #3855

Closed
roossienb opened this issue Mar 18, 2024 · 6 comments
Labels
need more info Further information is requested

Comments

@roossienb
Copy link

roossienb commented Mar 18, 2024

Software:

  • Visual Studio 2022
  • Libman to load libraries
  • maplibre-gl 4.1.1
  • Firefox

I'm trying to reproduce this example in Typescript using this import example.

mymap.ts:

import { Map } from 'maplibre-gl';

new Map({
    container: 'map',
    style: 'https://demotiles.maplibre.org/style.json',
    center: [-74.5, 40],
    zoom: 9
});

index.htm:

    <script type="importmap" asp-append-version="true">
        {
          "imports": {
            "maplibre-gl": "https://unpkg.com/maplibre-gl/dist/maplibre-gl.js"
          }
        }
    </script>
    <script src="~/js-test/mymap.min.js" type="module" asp-append-version="true"></script>

Typescript compiles, but the browser gives the error import not found: Map

I've also tried (while using new maplibregl.Map() instead of new Map()):

// import not found: default
import maplibregl from 'maplibre-gl';

// import not found: maplibregl
import { maplibregl } from 'maplibre-gl'

// maplibregl.Map is not a constructor
import * as maplibregl from 'maplibre-gl'

Is something going wrong with the exports?

@HarelM
Copy link
Member

HarelM commented Mar 18, 2024

How do you compile an html file as typescript? This is strange...
Please provide stackblitz/jsbin reproduction.

@HarelM HarelM added the need more info Further information is requested label Mar 18, 2024
@roossienb
Copy link
Author

How do you compile an html file as typescript? This is strange... Please provide stackblitz/jsbin reproduction.

I compile the mymap.ts file to mymap.min.js and have that javascript file loaded by the html page.

@neodescis
Copy link
Collaborator

neodescis commented Mar 18, 2024

I think most folks also use some sort of bundler, but it seems like this should work. I've never used an importmap though. It might be somewhat complicated by "Map" already existing though. Have you tried aliasing the import?

import { Map as MapLibre } from 'maplibre-gl'

@roossienb
Copy link
Author

roossienb commented Mar 18, 2024

importmap is needed to be able to use a CDN, otherwise the import always looks for a local file.

I've simplified the problem, because it looks like it has nothing to do with typescript.

<html>
<head>
    <script type="importmap">
        {
            "imports": {
                "maplibre-gl": "https://unpkg.com/maplibre-gl@4.1.1/dist/maplibre-gl.js"
            }
        }
    </script>
    <script type="module">
        import { Map } from 'maplibre-gl';

        new Map({
            container: 'map',
            style: 'https://demotiles.maplibre.org/style.json', 
            center: [-74.5, 40],
            zoom: 9
        });
    </script>
</head>
<body>
    <section id="map">
    </section>
</body>
</html>

This should work, but doesn't as it cannot find 'Map'. An alias doesn't solve it either.

import * as M from 'maplibre-gl'

results in a module M that exists, but that module does not contain M.Map() or M.default.Map().

I think something is going wrong with exports in the maplibre-gl library.

@HarelM
Copy link
Member

HarelM commented Mar 19, 2024

The following will work due to how maplibre package is built:

<html>
<head>
    <script type="importmap">
        {
            "imports": {
                "maplibre-gl": "https://unpkg.com/maplibre-gl@4.1.1/dist/maplibre-gl.js"
            }
        }
    </script>
    <script type="module">
        import 'maplibre-gl';

        new maplibregl.Map({
            container: 'map',
            style: 'https://demotiles.maplibre.org/style.json', 
            center: [-74.5, 40],
            zoom: 9
        });
    </script>
</head>
<body>
    <section id="map">
    </section>
</body>
</html>

@HarelM
Copy link
Member

HarelM commented Mar 19, 2024

@maplibre maplibre locked and limited conversation to collaborators Mar 19, 2024
@HarelM HarelM converted this issue into discussion #3859 Mar 19, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
need more info Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants