最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - using .mjs file extension js modules - Stack Overflow

programmeradmin1浏览0评论

I have a problem concerning .js and .mjs file extensions. When I use <script type = "module" src="test.js"></script> as a html script import statement everything works fine.

But if I use <script type = "module" src="test.mjs"></script> as import statement, I get the following error message:

"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."

I use a local XAMPP Apache Webserver for testing purposes. Does somebody know, how I can tell my browser that .mjs is a javascript file?

Thank you for your help!

I have a problem concerning .js and .mjs file extensions. When I use <script type = "module" src="test.js"></script> as a html script import statement everything works fine.

But if I use <script type = "module" src="test.mjs"></script> as import statement, I get the following error message:

"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."

I use a local XAMPP Apache Webserver for testing purposes. Does somebody know, how I can tell my browser that .mjs is a javascript file?

Thank you for your help!

Share Improve this question asked May 14, 2020 at 12:21 MetalheadMetalhead 912 silver badges7 bronze badges 4
  • You've to configure the mime_module in httpd.conf file: AddType application/javascript .mjs. – Teemu Commented May 14, 2020 at 12:30
  • Can I do it somewhere in the html as well? – Metalhead Commented May 14, 2020 at 12:45
  • No, you can't, you need to update the server configuration. – Teemu Commented May 14, 2020 at 12:49
  • It's also possible to get the same error, if the module is not found (i.e. you've an incorrect path/filename), when the server responses with an error page (which is usually html). I'm not aware how to fix this. – Teemu Commented May 14, 2020 at 12:55
Add a ment  | 

1 Answer 1

Reset to default 4

If the server does not respond with the correct MIME-Type for .mjs or other files, you may be able to add this Type in your .htaccess with

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

That solved the problem for me.

But I also decided to use the .js extension again for my modules, because I think the time for .mjs as extension has not e yet.

发布评论

评论列表(0)

  1. 暂无评论