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

javascript - es6 import using.mjs suffix fails with MIME error - Stack Overflow

programmeradmin1浏览0评论

In the latest chrome browser

import foo from '../dist/foo.mjs'

fails with

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.

But if I copy the file to foo.js

import foo from '../dist/foo.js'

works!

I am using the mac os x built-in Apache server with localhost paths and the latest chrome.

So somehow es6 imports are sensitive, in the browser, to the preferred node suffix of .mjs.

The import is in a html file with <script type="module"> rather than a separate module file, but I doubt that is the problem.

Is there a solution to this?.

In the latest chrome browser

import foo from '../dist/foo.mjs'

fails with

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.

But if I copy the file to foo.js

import foo from '../dist/foo.js'

works!

I am using the mac os x built-in Apache server with localhost paths and the latest chrome.

So somehow es6 imports are sensitive, in the browser, to the preferred node suffix of .mjs.

The import is in a html file with <script type="module"> rather than a separate module file, but I doubt that is the problem.

Is there a solution to this?.

Share Improve this question edited May 5, 2019 at 18:57 oguz ismail 51.1k16 gold badges60 silver badges79 bronze badges asked May 5, 2019 at 18:56 backspacesbackspaces 3,9627 gold badges38 silver badges62 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You need to usa an .htaccess file or extend your Apache config file with the following htaccess directive in order for the server to output files with the mjs extension with the correct MIME type:

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

Of course, mod_mime needs to be installed and enabled for this to work.

发布评论

评论列表(0)

  1. 暂无评论