Why can I import a script file from a local directory but I can't when the type is a module?

47 Views Asked by At

Let's say you create a project folder named 'project' in your desktop and inside of it a simple index.html file. Now, you want to create a script and so to not polute your index.html, you code your script inside a script.js file and then import it in your index.html using <script src="script.js"></script>.

This works flawlessly! However, if you just so wish to make it an ES6 module by using type="module", you'll face a CORS policy block error and the script will not load.

Why does this happen? Is there any way to use ES6 modules in local files without coding directly in a script tag within the html file? Is this ever going to be supported?

Using a bundler/compiler is out of question in this scenario as it is inteded to be as simple as possible, but using ES6.

0

There are 0 best solutions below