How To Fix Failed To Load Module Script: Expected A Javascript Module Script But The Server Responded With A Mime Type Of “text/plain”. Strict Mime Type Checking Is Enforced For Module Scripts Per Html Spec

I include an external javascript file in my Html file, this javascript file is written by me also. The Html file name is test.html, the javascript file name is test.js. I upload the two files to a local web browser ( if not upload the file test.html, test.js to a web server, you will encounter an error, you can see the reference section at the end of this article to learn more ). And when I access the test.html with the URL http://localhost:8000/test.html, it shows the error message Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of “text/plain”. This article will tell you how to fix it.

1. How To Fix Failed To Load Module Script: Expected A Javascript Module Script But The Server Responded With A Mime Type Of “text/plain”.

  1. Below is the detailed error message.
    test.js:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.
  2. Below is the test.html file source content code.
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Example<title>
    <script type="module" src="test.js" ></script>
    </head>
    <body>
    ......
    </body>
    </html>
  3. The reason is that the type attribute’s value ( “module” ) of the source code <script type=”module”></script>.
  4. You should change the type attribute’s value to “text/javascript” like below.
    <script type="text/javascript" src="test.js" ></script>
  5. Now when you browse the test.html file in a web browser again, the error will disappear.

References

  1. How To Fix Error Access To Script At 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.

2 thoughts on “How To Fix Failed To Load Module Script: Expected A Javascript Module Script But The Server Responded With A Mime Type Of “text/plain”. Strict Mime Type Checking Is Enforced For Module Scripts Per Html Spec”

  1. Wow. Your answer to prevent an error with some code is, “don’t use that code.”

    The module type is needed in order to defer and execute scripts is the order they’re called, so I personally need it. Looking for an actual solution.

  2. I have been attempting to find a solution since last night thank you man i did not know how to change the type XD

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.