Javascript 相对引用必须以“或”开头/&引用&引用/&引用;,或者说/&引用;。在实时服务器上运行时

Javascript 相对引用必须以“或”开头/&引用&引用/&引用;,或者说/&引用;。在实时服务器上运行时,javascript,leaflet,maps,Javascript,Leaflet,Maps,我收到一个错误uncaughttypeerror:解析模块说明符“传单”失败。相对引用必须以“/”、“/”或“./”开头。 我正在尝试使用软件包运行我的应用程序 我正在使用Google Chrome作为浏览器,我正在启用CORS,否则我将收到与CORS相关的错误 将我指向此代码的第一行时出错 import "./styles.css"; import L from "leaflet"; import "leaflet/dist/leafl

我收到一个错误
uncaughttypeerror:解析模块说明符“传单”失败。相对引用必须以“/”、“/”或“./”开头。

我正在尝试使用软件包运行我的应用程序

我正在使用Google Chrome作为浏览器,我正在启用CORS,否则我将收到与CORS相关的错误

将我指向此代码的第一行时出错

    import "./styles.css";
import L from "leaflet";
import "leaflet/dist/leaflet.css";

const icon = L.icon({
  iconSize: [25, 41],
  iconAnchor: [10, 41],
  popupAnchor: [2, -40],
  iconUrl: "https://unpkg.com/leaflet@1.6/dist/images/marker-icon.png",
  shadowUrl: "https://unpkg.com/leaflet@1.6/dist/images/marker-shadow.png"
});

var map = L.map("map", {
  preferCanvas: true
}).setView([51.505, -0.09], 3);

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);

fetch("https://gbfs.urbansharing.com/oslobysykkel.no/station_information.json")
  .then((response) => response.json())
  .then((responseData) => {
    console.log(responseData.data);
    const stations = responseData.data.stations;

    const layerGroup = L.featureGroup().addTo(map);

    stations.forEach(({ lat, lon, name, address }) => {
      layerGroup.addLayer(
        L.marker([lat, lon], { icon }).bindPopup(
          `Name: ${name}, Address: ${address}`
        )
      );
    });

    map.fitBounds(layerGroup.getBounds());
  });
这是我的
package.json

    {
  "name": "oslotest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "leaflet": "^1.7.1",
    "node": "^14.13.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
如您所见,我已通过NPM安装了
传单

当我在沙箱中运行这段代码时,根本没有问题


改用
npm start
运行站点。对我有用。编辑:等待:codesandboxes的package.json不同,它使用的是包裹;不能将已安装的模块直接用于live server。您可以在现代浏览器中使用
import
,但您需要将
type=“module”
添加到脚本中,并从在线软件包中导入。@ChrisG很抱歉,您可以解释一下type=“module”的更多信息以及我想在哪里使用它吗?不要进入兔子洞,只要用codesandbox中的package.json替换您的package.json即可,运行
npm安装
然后
npm启动
@ChrisG非常感谢你,帮了我大忙!
    {
  "name": "oslotest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "leaflet": "^1.7.1",
    "node": "^14.13.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}