首页 > 解决方案 > Cannot use import statement outside a module(Vanilla Javascript)

问题描述

when i tried to import a class from one js file to another i got this error

Uncaught SyntaxError: Cannot use import statement outside a module

when i added type="module" to my script tags i got this error

Access to script at 'file link' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

any help on how to fix the error ?

标签: javascript

解决方案


这可以通过将 type="module" 添加到我的脚本标签并从在 localhost 上运行的 HTTP 服务器提供我的 Web 文件而不是直接打开 html 文件来解决

这些答案很有帮助:

如何在本地主机上运行 html 文件?


推荐阅读