且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从本地文件夹导入 type=module 的脚本会导致 CORS 问题

更新时间:2023-12-05 09:08:40

我已经用谷歌搜索了几个小时,发现我可以通过服务器(如 node.js)托管我的应用程序

I've googled that for hours and found out that I can host my app by a server (like node.js)

是的

然后允许 CORS.

由于它将是同源,因此您不需要 CORS.

Since it will be Same Origin, you won't need CORS.

但是我不想要任何服务器.我只想要一个简单的 html 和一个 js 文件.

However I don't want any server. I want just a simple html and a js file.

那你就不能使用浏览器端的 ES6 模块了.

Then you can't use browser-side ES6 modules.

您需要先编写代码以不使用模块,或者使用 Webpack 或 Rollup 之类的东西将代码转换为以后不使用模块.

You'll need to either write the code to not use modules in the first place, or use something like Webpack or Rollup to convert the code to not use modules afterwards.