"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
I'm using ArcGIS JSAPI 4.12 and wish to use Spatial Illusions to draw military symbols on a map.
When I add milsymbol.js
to the script, the console returns error
Uncaught SyntaxError: Cannot use import statement outside a module`
so I add type="module"
to the script, and then it returns
Uncaught ReferenceError: ms is not defined
Here's my code:
<link rel="stylesheet" href="https://js.arcgis.com/4.12/esri/css/main.css">
<script src="https://js.arcgis.com/4.12/"></script>
<script type="module" src="milsymbol-2.0.0/src/milsymbol.js"></script>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/MapImageLayer",
"esri/layers/FeatureLayer"
], function (Map, MapView, MapImageLayer, FeatureLayer) {
var symbol = new ms.Symbol("SFG-UCI----D", { size: 30 }).asCanvas(3);
var map = new Map({
basemap: "topo-vector"
});
var view = new MapView({
container: "viewDiv",
map: map,
center: [121, 23],
zoom: 7
});
});
</script>
So, whether I add type="module"
or not, there are always errors. However, in the official document of Spatial Illusions, there isn't any type="module"
in the script. I'm now really confused. How do they manage to get it work without adding the type?
File milsymbol.js
import { ms } from "./ms.js";
import Symbol from "./ms/symbol.js";
ms.Symbol = Symbol;
export { ms };
Solution 1:
I got this error because I forgot the type="module"
inside the script tag:
<script type="module" src="milsymbol-2.0.0/src/milsymbol.js"></script>
Solution 2:
Update For Node.js / NPM
Add "type": "module"
to your package.json
file.
{
// ...
"type": "module",
// ...
}
Note: When using modules, if you get ReferenceError: require is not defined
, you'll need to use the import
syntax instead of require
. You can't natively mix and match between them, so you'll need to pick one or use a bundler if you need to use both.
Solution 3:
It looks like the cause of the errors are:
-
You're currently loading the source file in the
src
directory instead of the built file in thedist
directory (you can see what the intended distributed file is here). This means that you're using the native source code in an unaltered/unbundled state, leading to the following error:Uncaught SyntaxError: Cannot use import statement outside a module
. This should be fixed by using the bundled version since the package is using rollup to create a bundle. -
The reason you're getting the
Uncaught ReferenceError: ms is not defined
error is because modules are scoped, and since you're loading the library using native modules,ms
is not in the global scope and is therefore not accessible in the following script tag.
It looks like you should be able to load the dist
version of this file to have ms
defined on the window
. Check out this example from the library author to see an example of how this can be done.
Solution 4:
I was also facing the same issue until I added the type="module" to the script.
Before it was like this
<script src="../src/main.js"></script>
And after changing it to
<script type="module" src="../src/main.js"></script>
It worked perfectly.