Saturday, 23 July 2016

javascript - Trouble with ES6 Modules



Solved: Fixing the MIME types of my server fixed the problem. I had forgotten that I messed with them myself some time ago. Special Thanks to @Sidney, @estus and @Josh Lee for helping me out.






Once I found a working live-demo referenced on the MDN of ES6 Modules that just works in my current version of Chrome, I wanted to try to experiment with modules. Sadly I can't get anything module related to execute, even tho the live-demo works just fine. I even copied both files (index.html, utils.js) onto a directory on my server trying to recreate the live-demo exactly, but the thing still won't run even one single line of code. What am I missing?
Could someone give me some hints about when module scripts execute and why mine doesn't?



tl;dr: I found a working example of ES6 modules, but attempting to recreate it on my own local server does not work.




[Edit:] Yes, the console is set to "Hide all". Both sites show a error for a missing favicon.ico though, so it has nothing to do with my problem.



[Edit:] The article referenced by the MDN, containing the live-demo.



[Update:] It seems that the problem is with an incorrect MIME-type given by my local server when getting the module.






index.html/test.htm:










utils.js:



export function addTextToBody(text) {
const div = document.createElement('div');
div.textContent = text;
document.body.appendChild(div);
}






mine:
Mine



live-demo:
enter image description here


Answer



The error message here is





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




This is a new requirement for web development which thus far could mostly get away with incorrect MIME types without much trouble.



Two quick web servers for local development which I know to have reasonable MIME handling are:




  1. Python's: python3 -m http.server (see 1).

  2. Node's http-server: npm i -g http-server && http-server (see 2).




In your case, the error message is not being shown, indicated by




Hide all […] 1 item hidden by filters




Fix this by clicking ‘Hide all’ and choosing ‘Default’ (and you may wish to set this to ‘All levels’ while working). Or reset the devtools to its default state:





  1. Press F1 in the devtools (or choose menu > Settings).

  2. Scroll to the bottom and click ‘Restore defaults and reload’.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...