9
votes

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

I get this error in chrome after trying to add this line to my HTML code:

<script type="module">import * as hello from './__target__/hello.js'; window.hello = hello;</script>
<!-- From the official documentation of Transcrypt -->

I've been trying to fix for hours, someone suggested to change the type to text/javascript and to use the src tag (src = './__ target__/hello.js') but I need some imports in hello.js

FIXED: Ok I was starting the server with 'python -m http.server' from command line, I just replaced it with this python2 script:

#Use to create local host
import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
Handler.extensions_map.update({
    ".js": "application/javascript",
});

httpd = SocketServer.TCPServer(("", PORT), Handler)

print ("Serving at port", PORT)
print(Handler.extensions_map[".js"])
httpd.serve_forever()
2
The error you show is a server-side error: the server should be telling your browser that this is a js file, not a plain text file. So start by putting your script in a file (don't inline it) and then link to that .js file with a <script src="..." type="module"></script>. If that yields the same error, your server is not sending files with their correct mime-type, and you should fix that (almost every server can do this for you out-of-the-box, so just google for how to configure yours to correctly add the mime type header)Mike 'Pomax' Kamermans
Are you sure that you are including the right file?some
I'm using python -m http.server to try my application, how should I setup content headers and mime-types?Fabrizio Apuzzo
@Mike'Pomax'Kamermans , I am getting the same issue after building the same application for android, how could I change the mime type if the program is not working on a server?Fabrizio Apuzzo

2 Answers

9
votes

This question (and the script involved) really helped me a lot. I have modified the script provided in the answer to make it compatible with python3.

#Use to create local host
import http.server
import socketserver

PORT = 1337

Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
      ".js": "application/javascript",
});

httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()

The notable changes are:-

  1. Changed SimpleHTTPServer to http.server. From python3 onwards , SimpleHTTPServer has become a part of http.server

  2. Changed SocketServer module to socketserver

The functions are backward compatible.

3
votes

Your web server is serving hello.js with Content-Type: text/plain, which is disallowed by the browser because it's not JavaScript.

You need to update your web server configuration so that it serves that file (and presumably all *.js files) with Content-Type: application/javascript.