Skip to content

SyntaxError: Cannot use import statement outside a module #130799

Closed Answered by luiisp
scr6w asked this question in Programming Help
Discussion options

You must be logged in to vote

This problem is caused by the fact that JavaScript modules require a specific way to be loaded into HTML files. To use import and export statements, you need to ensure that your script is treated as a module. You can do this by adding the type="module" attribute to your <script> tag in the HTML file.

So when importing the script into HTML it would look like this👍:
<script type="module" src="script.js"></script>

Result:

It even seems like this has already been discussed here on the github community

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@PPQAQ
Comment options

Answer selected by scr6w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Programming Help Programming languages, open source, and software development.
3 participants