Home >Web Front-end >JS Tutorial >Why Do I Get 'Uncaught SyntaxError: Cannot use import statement outside a module' When Importing ECMAScript 6 Modules?
When trying to import the ECMAScript 6 module milsymbol.js into an ArcGIS JSAPI project, you may encounter the error "Uncaught SyntaxError: Cannot use import statement outside a module." This guide provides a solution to this error.
The error occurs because the milsymbol.js script requires a module environment to use the import statement. Without a module environment, the browser will not recognize the syntax.
To enable a module environment in the ArcGIS JSAPI project, you can modify the script tag that includes milsymbol.js by adding the type="module" attribute:
<script type="module" src="milsymbol-2.0.0/src/milsymbol.js"></script>
This will create a module environment for the milsymbol.js script, allowing it to use the import statement.
If you are working with Node.js/NPM, you can also configure the package.json file to use the module syntax:
{ // ... "type": "module", // ... }
This will enable the module syntax throughout your project.
Once the module environment is configured, you can use the import syntax to reference the ms object from milsymbol.js:
import { ms } from "milsymbol-2.0.0/src/milsymbol.js";
Note that you will need to use the import syntax instead of the require syntax when working with modules.
The above is the detailed content of Why Do I Get 'Uncaught SyntaxError: Cannot use import statement outside a module' When Importing ECMAScript 6 Modules?. For more information, please follow other related articles on the PHP Chinese website!