Import declarations may only appear at top level of a module

自古美人都是妖i 提交于 2020-06-27 08:53:10

问题


I am trying to setup OpenLayers on my computer, and am following the directions here: http://openlayers.org/en/latest/doc/tutorials/bundle.html

When I try to run it in my browser I get error:
import declarations may only appear at top level of a module

How do I fix that?

index.js:

import 'ol/ol.css';
import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';

const map = new Map({
  target: 'map',
  layers: [
    new TileLayer({
      source: new OSM()
    })
  ],
  view: new View({
    center: [0, 0],
    zoom: 0
  })
});

回答1:


The error clearly states that somewhere in your .js files there is an import statement:

import something from 'some-package'

And that import statement is not at the very top of the module e.g.:

someExpressionHere(); // This must be moved below import statement
import something from 'some-package'

UPD: After chatting with OP, the actual problem was not building the project (not bundling), when there was a css module import.



来源:https://stackoverflow.com/questions/53350864/import-declarations-may-only-appear-at-top-level-of-a-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!