【发布时间】:2021-04-24 08:02:33
【问题描述】:
我正在使用本地服务器进行一个项目。
在这个项目中,我正在使用模块,所以我的index.html 包含:
<script src="js/index.js" type="module"></script>
我的index.js 包含:
import {} from "/js/randomModule.js";
import {} from "/js/randomModule.js";
...
一切运行完美,但是当我想直接在浏览器上使用该项目时,没有服务器,这就是问题:
Access to the script at 'file:///C:/Users/myFolder/js/index.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
index.html:54
GET file:///C:/Users/myFolder/js/index.js net::ERR_FAILED
那么,有没有办法在没有服务器的情况下直接在浏览器中使用 javascript type="module"?
【问题讨论】:
-
考虑使用简单的服务器而不是直接在浏览器中打开文件?如果您使用的是 unix/mac 并安装了节点,我建议所有平台使用
npx servenpmjs.com/package/serve 或 browsersync.io -
或在您的编辑器或浏览器中安装实时预览扩展
标签: javascript browser server module