Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain"

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()
like image 487
Fabrizio Apuzzo Avatar asked Jan 25 '20 11:01

Fabrizio Apuzzo


1 Answers

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.

like image 160
Echo Avatar answered Sep 16 '22 11:09

Echo