I'm currently trying to load a Leaflet map in a personal project. but my code does not execute because of an import problem. i installed leaflet type definitions with the following command
npm install --save @types/leaflet
it has one dependency so ive installed geojson too
npm install --save @types/geojson
this is part of my tsconfig.json file
{
"compilerOptions": {
"typeRoots": ["node_modules/@types"],
"rootDir": ".",
"outDir": "build",
"target": "es2018",
"lib": [
"es2018",
"dom"
],
"types": [
"leaflet",
"geojson"
]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
i'm following the basic example in the documentation of leaflet, but instead of writing inline code i made a class.
this is a part of the index where i load all my js files, the map div is inside the body (not shown here).
<link rel="stylesheet" type="text/css" href="ts\node_modules\leaflet\dist\leaflet.css">
<script src="ts\node_modules\leaflet\dist\leaflet.js"></script>
<script src="./ts/build/src/mapsmanager.js" type="module"></script>
<script type="module">
// Import classes
import { MapsManager } from './ts/build/src/mapsmanager.js';
initialize();
function initialize()
{
/* ADD MAP SCRIPT */
let mapsman = new MapsManager();
}
</script>
this is my mapsmanager.ts file where i get the error from
import * as Leaf from 'leaflet';
export class MapsManager
{
/*DO STUFF*/
}
this line
import * as Leaf from 'leaflet';
gives me the following runtime error:
TypeError: Error resolving module specifier: leaflet
i've also tried to load leaflet as a module
<script src="ts\node_modules\leaflet\dist\leaflet.js" type="module"></script>
but it didn't work, it gave me another runtime error:
TypeError: t is undefined (inside leaflet.js)
IMPORTANT without using classes and by using inline JS inside the index the map works fine. it has probably something to do with the project configuration/declarations/definitions/modules stuff or maybe even leaflet.
this is part of my package.json
"files": [
"build",
"src"
],
"devDependencies": {
"gts": "^2.0.0",
"typescript": "~3.8.0",
"@types/node": "^10.0.3"
},
"dependencies": {
"@types/geojson": "^7946.0.7",
"@types/leaflet": "^1.5.12",
"leaflet": "^1.6.0"
}
I've also tried to add a index.d.ts file where i put a declaration of the leaflet module because before leaflet i tried to use Google Maps JS API which uses this method but it didn't work.
I hope my description of the problem have been exhaustive
import L from 'leaflet';
does not work? – kboul