如何修复“未捕获的语法错误:导入声明只能出现在模块的顶层”?

问题描述 投票:0回答:1

我的 Firefox 版本是 188,所以我知道这不是版本问题。每当我“检查”然后控制台时,我都会收到以下错误。

未捕获的语法错误:导入声明只能出现在模块的顶层'

我在这个项目中总共只使用了三个文件和一个文件夹:“index.html”、“logic.js”和一个“data”文件夹,其中包含我的“trueData.json”文件。

索引html文件代码:

 <head>  
   <meta charset="utf-8">
 <title>Basic Map</title>

<!-- Leaflet CSS -->  
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin="" />

 <!-- Leaflet JavaScript code -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>

<!-- Our CSS -->
<link rel="stylesheet" type="text/css" href="style.css">

 <!-- D3 library -->
 <script src="https://d3js.org/d3.v7.min.js"></script>
  </head>

<body>
 <!-- The div where we'll insert our map -->
<div id="map"></div>

<!-- JavaScript files -->
<script type = "module" src = "./data/trueData.json"></script>
 <script type = "text/javascript" src = "logic.js"></script>
<!-- <script src = "./data/trueData.js" type = "module"></script>
<script src = "logic.js" type = "text/javascript"></script> -->

<!-- <script type="text/javascript" src="data/trueData.js"> 
</script> -->

  </body>

</html>

logic.js代码:

import data from './data/trueData.json';
console.log(data);


let myMap = L.map("map", {
center: [40.7, -73.95],
 zoom: 11
 });

 //Adding the tile layer
 L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 
 {
 attribution: '&copy; <a 
 href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> 
 contributors'
  }).addTo(myMap);

 
  //testing output of trueData variables
  d3.json("trueData.json").then(function (data) {
 let regionNameVar = data.RegionName;
 let countyNameVar = data.CountyName;
  
  console.log(regionNameVar);
  });
  
  
  //testing output of trueData variables
d3.json("trueData.json", function (d) { 

console.log(d); 
});

//testing output of trueData variables
function (ba) {
console.log(ba.RegionName); 
  //return da.RegionName;
 }
  
 //activates ba function
 ba();
 
 //gets region name and countyname from json and stores them into variables
let regionNameVar = trueData.RegionName;
let countyNameVar = trueData.CountyName;
 
  //tests to see if data from dataTest is even being read by above variables
 console.log(regionNameVar);
 
 let queryKey = trueData[regionNameVar, countyNameVar];
 
 console.log(`test of queryKey: + ${queryKey}`);
 metroArray = []
 
 //activates test call function
 testCall();

 //activates setLonandLat function
 setLonAndLat();
  
//test calls the setLonAndLat function to see if its working
 function testCall(paramOne, paramTwo)
{
let innerVar = paramOne;
let innerVarTwo = paramTwo;
setLonAndLat(innerVar, innerVarTwo);
console.log(innerVar, innerVarTwo);
}
 
//sets the region and county name to a array
function setLonAndLat(queryKey)

metroArray = [queryKey];
return metroArray;
console.log(metroArray);
 
 }

trueData.json 有 25057 行代码,所以我只关心显示其中的前两个对象;如果有人想复制我的错误,请删除第二个对象的对象逗号末尾,以便于复制和粘贴。

{
  "Malden": {
     "RegionID": "12519",
     "SizeRank": "26884",
     "RegionName": "Malden",
     "RegionType": "city",
     "StateName": "NY",
     "State": "NY",
     "Metro": "Kingston, NY",
     "CountyName": "Ulster County",
     "CountyLat": "41.6370",
     "CountyLng": "-74.2633",
     "RegionLat": "42.0951",
     "RegionLng": "-73.9351",
     "2013-01-31": "186572.56047947207",
     "2013-02-28": "186467.29556400285",
     "2013-03-31": "186074.57716018287",
     "2013-04-30": "185635.1640155002",
     "2013-05-31": "186010.12520848273",
     "2013-06-30": "187975.30724738076",
     "2013-07-31": "190406.9789123678",
     "2013-08-31": "191625.49423243987",
     "2013-09-30": "191041.81812376133",
     "2013-10-31": "190053.85598834968",
     "2013-11-30": "189722.97893962348",
     "2013-12-31": "190734.81692107613",
     "2014-01-31": "191396.99955092176",
     "2014-02-28": "191429.1480060797",
     "2014-03-31": "190880.24405020158",
     "2014-04-30": "190819.63631199996",
     "2014-05-31": "191595.73943507674",
     "2014-06-30": "192906.68965757024",
     "2014-07-31": "194322.66931993692",
     "2014-08-31": "194965.9291355007",
     "2014-09-30": "195070.55879234037",
     "2014-10-31": "194417.30114411388",
     "2014-11-30": "194067.12800515708",
     "2014-12-31": "194027.22953101926",
     "2015-01-31": "194014.0506432962",
     "2015-02-28": "193940.18977302243",
     "2015-03-31": "193037.68769974026",
     "2015-04-30": "192563.2947740308",
     "2015-05-31": "191799.77050784734",
     "2015-06-30": "192088.2638476692",
     "2015-07-31": "193105.09186893818",
     "2015-08-31": "194947.9422328741",
     "2015-09-30": "196659.2494037546",
     "2015-10-31": "197709.37158381465",
     "2015-11-30": "197792.23926520208",
     "2015-12-31": "197369.05703972772",
     "2016-01-31": "196475.09453886576",
     "2016-02-29": "195705.58312583077",
     "2016-03-31": "194667.45387350838",
     "2016-04-30": "193986.80660696104",
     "2016-05-31": "193966.20171627344",
     "2016-06-30": "194921.9138781526",
     "2016-07-31": "196819.49565881662",
     "2016-08-31": "199215.01530802646",
     "2016-09-30": "200606.8993044314",
     "2016-10-31": "201159.39525581864",
     "2016-11-30": "201360.19772328224",
     "2016-12-31": "202961.44583408625",
     "2017-01-31": "204696.6032991211",
     "2017-02-28": "206233.39673536396",
     "2017-03-31": "206767.48763796338",
     "2017-04-30": "207340.77355663644",
     "2017-05-31": "207693.83597468937",
     "2017-06-30": "208718.23780868296",
     "2017-07-31": "210075.39666623354",
     "2017-08-31": "211983.93976344072",
     "2017-09-30": "213121.0324150533",
     "2017-10-31": "213790.21860589122",
     "2017-11-30": "213991.45851879838",
     "2017-12-31": "214164.66963280403",
     "2018-01-31": "214160.63285060457",
     "2018-02-28": "213825.39040034442",
     "2018-03-31": "214220.93643048606",
     "2018-04-30": "215270.4662032896",
     "2018-05-31": "216722.59327466742",
     "2018-06-30": "218997.1579472689",
     "2018-07-31": "222334.89433806503",
     "2018-08-31": "226248.52281421606",
     "2018-09-30": "229066.48839541688",
     "2018-10-31": "229578.94309948242",
     "2018-11-30": "229151.85617116585",
     "2018-12-31": "227875.6652430599",
     "2019-01-31": "226810.83775866078",
     "2019-02-28": "225960.74194094897",
     "2019-03-31": "225130.5763023439",
     "2019-04-30": "224430.5864168331",
     "2019-05-31": "223812.72272570923",
     "2019-06-30": "224002.58989607508",
     "2019-07-31": "226076.1518698038",
     "2019-08-31": "228430.35625609188",
     "2019-09-30": "231212.76066118575",
     "2019-10-31": "232434.72756024756",
     "2019-11-30": "233524.07396042926",
     "2019-12-31": "233775.8480031844",
     "2020-01-31": "233772.34263176905",
     "2020-02-29": "234435.65526816135",
     "2020-03-31": "235839.36095330623",
     "2020-04-30": "237853.75383952295",
     "2020-05-31": "238768.10156632948",
     "2020-06-30": "239441.59669473406",
     "2020-07-31": "240808.05860695822",
     "2020-08-31": "243843.14795766654",
     "2020-09-30": "249395.6683839067",
     "2020-10-31": "257317.9724735272",
     "2020-11-30": "266994.4618394244",
     "2020-12-31": "277284.13040641317",
     "2021-01-31": "286572.7616266039",
     "2021-02-28": "295668.3560786749",
     "2021-03-31": "303574.11073587346",
     "2021-04-30": "310713.9351621421",
     "2021-05-31": "318008.3877780768",
     "2021-06-30": "325716.28619680693",
     "2021-07-31": "331361.63164698094",
     "2021-08-31": "334518.9998655006",
     "2021-09-30": "335110.0523791407",
     "2021-10-31": "335968.10015043104",
     "2021-11-30": "336233.624480457",
     "2021-12-31": "336537.8738762678",
     "2022-01-31": "340409.80944632925",
     "2022-02-28": "347213.99651157815",
     "2022-03-31": "355865.27418430825",
     "2022-04-30": "363398.0165889586",
     "2022-05-31": "370381.6627964542",
     "2022-06-30": "377352.2061134926",
     "2022-07-31": "380576.6579456424",
     "2022-08-31": "379800.58556297625",
     "2022-09-30": "377351.20382578816",
     "2022-10-31": "375963.2346694334",
     "2022-11-30": "374860.89402319904",
     "2022-12-31": "370928.1408329743",
     "2023-01-31": "365382.867058195",
     "2023-02-28": "361234.2562083223",
     "2023-03-31": "358678.25507566147",
     "2023-04-30": "358366.0304610518",
     "2023-05-31": "358668.2097713373",
     "2023-06-30": "360887.84872389695",
     "2023-07-31": "364013.4779761943",
     "2023-08-31": "366531.29863288463",
     "2023-09-30": "368410.2372727434"
 },
 "Wallkill": {
     "RegionID": "27749",
     "SizeRank": "3782",
     "RegionName": "Wallkill",
     "RegionType": "city",
     "StateName": "NY",
     "State": "NY",
     "Metro": "Kingston, NY",
     "CountyName": "Ulster County",
     "CountyLat": "41.6370",
     "CountyLng": "-74.2633",
     "RegionLat": "41.6092",
     "RegionLng": "-74.1648",
     "2013-01-31": "218057.87372541023",
     "2013-02-28": "218617.5003372829",
     "2013-03-31": "218982.9075352271",
     "2013-04-30": "220261.8385379552",
     "2013-05-31": "221329.43855296331",
     "2013-06-30": "222295.59874303863",
     "2013-07-31": "222892.1280908118",
     "2013-08-31": "223333.7489902942",
     "2013-09-30": "222871.1465983206",
     "2013-10-31": "221638.47638018383",
     "2013-11-30": "219516.0006267724",
     "2013-12-31": "218758.72737439172",
     "2014-01-31": "218674.91102704022",
     "2014-02-28": "219343.2698973429",
     "2014-03-31": "220247.04137428917",
     "2014-04-30": "221226.9146917955",
     "2014-05-31": "223299.54628380472",
     "2014-06-30": "224286.42813002385",
     "2014-07-31": "224986.74680011606",
     "2014-08-31": "224344.37871368413",
     "2014-09-30": "223838.15997542386",
     "2014-10-31": "223353.5933100309",
     "2014-11-30": "222960.1135924319",
     "2014-12-31": "223102.16150158006",
     "2015-01-31": "223142.9729831762",
     "2015-02-28": "224018.01642055018",
     "2015-03-31": "224829.82901418442",
     "2015-04-30": "225623.3353431277",
     "2015-05-31": "226150.28790885382",
     "2015-06-30": "226137.29690590256",
     "2015-07-31": "226736.38357493866",
     "2015-08-31": "227811.22607374025",
     "2015-09-30": "229463.3096574622",
     "2015-10-31": "231135.61924834445",
     "2015-11-30": "231596.90958307104",
     "2015-12-31": "231854.10099279127",
     "2016-01-31": "231213.119534813",
     "2016-02-29": "230621.70320757132",
     "2016-03-31": "229692.83487180388",
     "2016-04-30": "229400.2373847138",
     "2016-05-31": "230074.5628071644",
     "2016-06-30": "230954.85230767526",
     "2016-07-31": "231650.53492601358",
     "2016-08-31": "232339.5152870851",
     "2016-09-30": "232923.61708622004",
     "2016-10-31": "233861.99892187407",
     "2016-11-30": "233815.8745999483",
     "2016-12-31": "233804.96215612473",
     "2017-01-31": "233306.42380688956",
     "2017-02-28": "233580.1104253979",
     "2017-03-31": "234496.2601233842",
     "2017-04-30": "235937.87574272475",
     "2017-05-31": "237659.96392129353",
     "2017-06-30": "238463.6096529977",
     "2017-07-31": "239105.27726949658",
     "2017-08-31": "239971.67355849617",
     "2017-09-30": "241384.88488923453",
     "2017-10-31": "242841.86835000917",
     "2017-11-30": "243294.66777993133",
     "2017-12-31": "243666.64014889149",
     "2018-01-31": "243924.77993854074",
     "2018-02-28": "244032.98928037845",
     "2018-03-31": "244570.8306729896",
     "2018-04-30": "245414.58991455392",
     "2018-05-31": "247418.34883302823",
     "2018-06-30": "249623.31859131422",
     "2018-07-31": "252025.21746509383",
     "2018-08-31": "254156.07215353157",
     "2018-09-30": "255940.73537354733",
     "2018-10-31": "256580.44124393517",
     "2018-11-30": "256487.3483277522",
     "2018-12-31": "255220.50038828494",
     "2019-01-31": "253736.9336637354",
     "2019-02-28": "252965.99766387962",
     "2019-03-31": "253638.05200377505",
     "2019-04-30": "255094.36440346102",
     "2019-05-31": "256262.7997582086",
     "2019-06-30": "256851.45658629108",
     "2019-07-31": "258448.14818130902",
     "2019-08-31": "260485.62228006078",
     "2019-09-30": "262856.13957999094",
     "2019-10-31": "264135.21192612586",
     "2019-11-30": "265534.241508774",
     "2019-12-31": "267098.19467501255",
     "2020-01-31": "269814.4932761415",
     "2020-02-29": "273319.5175975308",
     "2020-03-31": "276540.0454889952",
     "2020-04-30": "278948.8840004499",
     "2020-05-31": "279665.104744891",
     "2020-06-30": "280562.54009604687",
     "2020-07-31": "281994.1747311656",
     "2020-08-31": "285110.84333640593",
     "2020-09-30": "289873.78787867626",
     "2020-10-31": "296548.0013607284",
     "2020-11-30": "304245.82110670855",
     "2020-12-31": "311725.8046700925",
     "2021-01-31": "317927.5848767166",
     "2021-02-28": "323653.7613747743",
     "2021-03-31": "329082.28929438273",
     "2021-04-30": "334304.6380548199",
     "2021-05-31": "339659.78182426916",
     "2021-06-30": "345108.9650838513",
     "2021-07-31": "349594.0561062938",
     "2021-08-31": "352424.9834341823",
     "2021-09-30": "353776.65961686027",
     "2021-10-31": "355363.8225251048",
     "2021-11-30": "357334.97357605846",
     "2021-12-31": "359648.90843731986",
     "2022-01-31": "363610.49696678465",
     "2022-02-28": "369214.85628183436",
     "2022-03-31": "376207.43937485054",
     "2022-04-30": "382791.2339919249",
     "2022-05-31": "388529.33796439064",
     "2022-06-30": "393774.7377336207",
     "2022-07-31": "397402.993194211",
     "2022-08-31": "398749.6449352905",
     "2022-09-30": "398727.7445958664",
     "2022-10-31": "398376.3180978065",
     "2022-11-30": "398498.55295880855",
     "2022-12-31": "398703.75323963375",
     "2023-01-31": "398525.3134658613",
     "2023-02-28": "398725.3393337157",
     "2023-03-31": "398488.92591791716",
     "2023-04-30": "399277.47538338584",
     "2023-05-31": "400399.4727170441",
     "2023-06-30": "402629.50071645743",
     "2023-07-31": "405727.97138214594",
     "2023-08-31": "409253.4919683622",
     "2023-09-30": "413431.12705198536"
     }
 }

我为尝试解决此问题所做的工作:

  1. 查看了以下堆栈问题以尝试修复错误: 语法错误:导入声明只能出现在模块的顶层

语法错误:导入声明只能出现在模块的顶层

ES2015 导入在 Firefox 中不起作用(即使在顶层)

  1. 将“模块”添加到行> 在我第四次尝试解决该问题时,在 html 文件中。错误消息保持不变。

  2. 尝试使用 Bing AI,当我已经完成步骤 2 时,它建议执行步骤 2,并查找我的 Firefox 版本号。 Firefox 是最新的,因此logic.js 代码应该可以工作。错误仍然相同。

我不知道控制台除了该错误消息之外什么都不输出。

javascript json firefox
1个回答
0
投票

added 'module' to line <script type = "module" src = "./data/trueData.json"></script>
- 不需要让 json 文件导入模块, type="module" 的目的是允许 .js 文件有 import 语句。尝试将其添加到 html 中的 javascript 导入中 -
<script type = "module" src = "./logic.js"></script>

© www.soinside.com 2019 - 2024. All rights reserved.