40139037
40139037

Reputation: 3

Expected JS module, server responded with MIME?

I'm new to JS and I've been turning one of my scripts that was Node to be attached to a webpage for canvas graphics.

I've made it have a .mjs extension and seems to be mostly working, but the modules that were in the script are all broken. The specific error in the webpage console, once per module, is:

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

What does this mean and how do I fix it? It's also returning a 404 on my .env but that's less important.

The way I'm importing things into the script is import fetch from '/node_modules/node-fetch'; and in the HTML on the live server is <script type="module" src="script.mjs"></script>

if it matters, the script references all have status 301 and the modules have status 200 on the network tab. I'm hosting the webpage using the "Live Server" extension for VSCode.

Upvotes: 0

Views: 5396

Answers (1)

Ahmed Elhakim
Ahmed Elhakim

Reputation: 184

The error is due to that the server is serving the .mjs with wrong Content Type. So try to convert the .mjs to .js or use Http-server instead. As in this link mdn module guide it states that http-server and github pages work correctly with .mjs. You can also try to use a bundler such as Parcel or webpack.

Upvotes: 1

Related Questions