Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import declarations may only appear at top level of a module

Tags:

import

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
  })
});
like image 672
john-jones Avatar asked Oct 17 '22 10:10

john-jones


1 Answers

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.

like image 73
Nurbol Alpysbayev Avatar answered Oct 21 '22 04:10

Nurbol Alpysbayev