Currently working on a Blazor project where I want advanced mapping functionality, using the Leaflet.js library with typescript bindings.
I have added leaflet and @types/leaflet as node modules for allowing typescript support.
With everything ready and running, the browser console shows the following error:
Uncaught TypeError: Failed to resolve module specifier "leaflet". Relative references must start with either "/", "./", or "../".
There is one line generated at the top of the JS file:
import * as L from 'leaflet';
If I remove this line, everything works, but I can't remove it manually because it's generated automatically from my TS file, where it's needed.
I suspect my mistake is in tsconfig, or in the typescript file itself.
tsconfig:
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "ES6",
"strict": true,
"rootDir": "typescript",
"outDir": "wwwroot/scripts",
"esModuleInterop": true,
},
"exclude": [
"node_modules",
"wwwroot"
],
}
Typescript file:
import * as L from 'leaflet'
let map: L.Map;
let apiKey: string = "";
let mapStyle: string = 'saitken88/cl2vcjae400aw14qoiawg02c8'
let centreLatLong: L.LatLngExpression = [51.509865, -0.118092];
let mapOptions: L.MapOptions = {
minZoom: 6,
maxZoom: 9,
center: centreLatLong,
zoom: 6,
attributionControl: false,
};
function initMap(mapId: string) {
console.log('init map');
map = L.map(mapId, mapOptions).setView(centreLatLong, 13);
L.tileLayer(`https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}`, {
maxZoom: 18,
id: mapStyle,
tileSize: 512,
zoomOffset: -1,
accessToken: apiKey,
}).addTo(map);
}
Compiled JS file:
import * as L from 'leaflet';
let map;
let apiKey = "";
let mapStyle = 'saitken88/cl2vcjae400aw14qoiawg02c8';
let centreLatLong = [51.509865, -0.118092];
let mapOptions = {
minZoom: 6,
maxZoom: 9,
center: centreLatLong,
zoom: 6,
attributionControl: false,
};
function initMap(mapId) {
console.log('init map');
map = L.map(mapId, mapOptions).setView(centreLatLong, 13);
L.tileLayer(`https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}`, {
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: mapStyle,
tileSize: 512,
zoomOffset: -1,
accessToken: apiKey,
}).addTo(map);
}
//# sourceMappingURL=property-map.js.map
index.html scripts:
<script src="scripts/maps/leaflet.js" type="module"></script>
<script src="scripts/maps/property-map.js" type="module"></script>
Not sure if this is needed but this is my packake.json
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"type": "module",
"devDependencies": {
"@types/leaflet": "^1.7.9"
},
"dependencies": {
"leaflet": "^1.8.0"
}
}
I understand webpack might be a solution here, and have tried it, but frankly it seemed overly cumbersome for just getting one typescript file to compile nicely.
"module": "es2020",
or"module": "es6",
incompilerOptions
. See Specify what module code is generated.../
to as prefix, because local files need to accessed via relative path