19

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()
Fabrizio Apuzzo
  • 231
  • 1
  • 2
  • 12
  • 1
    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 ``. 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 Jan 25 '20 at 11:56
  • Are you sure that you are including the right file? – some Jan 25 '20 at 12:14
  • I'm using python -m http.server to try my application, how should I setup content headers and mime-types? – Fabrizio Apuzzo Jan 25 '20 at 16:23
  • @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 Jan 31 '20 at 11:53
  • Face the same issue. Yet strangely when I launch http.server with python 3.8.5, it is ok, but when I launch http.server with python 3.8.10 at another computer, the issue occurs. – Leslie Wong Apr 15 '22 at 02:12

6 Answers6

18

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.

iansedano
  • 6,169
  • 2
  • 12
  • 24
Echo
  • 570
  • 1
  • 7
  • 21
7

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you for the answer, but what if I am building it on android using cordova (so without any server)? In this case I get The server responded with a non-JavaScript MIME type of "" – Fabrizio Apuzzo Jan 31 '20 at 11:56
  • @FabrizioApuzzo - `application/javascript` is a JavaScript MIME type. Again: The server needs to serve the file correctly. – T.J. Crowder Jan 31 '20 at 12:34
  • Ok, but I am using a framework that converts html5 to native mobile applications, so I do not have any server to configure – Fabrizio Apuzzo Feb 01 '20 at 19:19
  • @FabrizioApuzzo - There's probably a server embedded in there (there usually is). You'll have to dive into the documentation. – T.J. Crowder Feb 02 '20 at 09:55
  • Hello, I'm a JS learner, I get this same error when I type this in console although my file is json: `import('https://raw.githubusercontent.com/ljharb/json-file-plus/master/package.json').then(m=> console.log(m));` What can I do? I also tried this: `a=document.createElement('script'); a.setAttribute('type','module'); a.textContent = "import * as name from 'https://raw.githubusercontent.com/ljharb/json-file-plus/master/package.json'; console.log(name);" document.body.append(a)` – Shayan Mar 19 '20 at 13:50
1

Another option is renaming the Javascript file to *.mjs instead of *.js. https://github.com/python/cpython/issues/75896

1

I am hosting my Angular compiled website with IIS and had to change the mime type in it for .js files from application/javascript to text/javascript, then it worked for some reason.

xinthose
  • 3,213
  • 3
  • 40
  • 59
0

In my case (Windows 10 + Django 2.2.25) it was windows registry Computer\HKEY_CLASSES_ROOT.js has a Content Type key set to text/plain. Changing it to application/javascript it worked again. Django use view.py and static.py to serve static file (static.serve) that uses mimetype (from Python library). mimetype.py call mimetypes.guess_type()

Cosmo
  • 1
  • 2
0

In my case, I removed the registry entry from:

Before

to

after

Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34