Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 modules in local files - The server responded with a non-JavaScript MIME type

I get this error:

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

The project is run from local files, eg.: file:///D:/path/project.html.

Works without problems in Firefox, but doesn't work in Google Chrome. I want to test it this way for development purposes - it's more comfortable than creating a server and remembering what port is it on.

like image 512
Tomáš Zato - Reinstate Monica Avatar asked Nov 21 '17 00:11

Tomáš Zato - Reinstate Monica


People also ask

Are ES6 modules supported?

As ES6 refers to a huge specification and browsers have various levels of support, "Supported" means at least 95% of the spec is supported.

What are ES6 modules?

Modules are the piece or chunk of a JavaScript code written in a file. JavaScript modules help us to modularize the code simply by partitioning the entire code into modules that can be imported from anywhere. Modules make it easy to maintain the code, debug the code, and reuse the piece of code.


1 Answers

A simple fix for me that wasn't listed here was this:

I had an import statement bringing an object from a different file, which I did via this line:

import { Cell } from './modules/Cell';

What broke the code and caused the MIME type error was not having .js appended to the end of ./modules/Cell.

The updated line fixed my problem:

import { Cell } from './modules/Cell.js';

like image 79
James Gould Avatar answered Oct 03 '22 23:10

James Gould