如何在 Apache Server 上使用 ES6 模块?

How to use ES6 modules on Apache Server?

我试图在 HTML 页面中导入 ES6 模块,但出现以下错误:

Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

此 html 页面托管在 Apache 服务器上,但为了开发我的应用程序,我使用了 WebStorm 本地服务器并且它可以正常工作。

这是我用来导入模块的代码:

<script type="module" src="js/TextAnalyzer.mjs" application=""></script>
<script type="module" src="js/Overlay.mjs"></script>
<script type="module" src="js/Main.mjs"></script>

我在 Chrome 和 Firefox 上都试过了,我遇到了同样的问题。

我该如何解决?

这里回答了这个问题。您基本上需要创建 .HTACCESS 文件和可以访问 index.htm 或 index.html 的项目根目录,并将此代码添加为其内容:

<IfModule mod_mime.c>
  AddType text/javascript js mjs
</IfModule>