Alex Gusev
Alex Gusev

Reputation: 1854

JavaScript: absolute URL as es-module address in imports

Is it possible to use absolute URLs for es6-modules import in browsers?

import something from 'https://example.com/js/lib/es6_module.mjs';

As I know, it is impossible for nodejs. I have the following error in this case:

Uncaught Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader. Received protocol 'https:'

Upvotes: 2

Views: 2732

Answers (2)

Alex Gusev
Alex Gusev

Reputation: 1854

Yes, it is possible. I've check it for browsers Chrome (94), Firefox (v93), Opera (v79).

This is HTML import:

<script type="module">
    import fn from 'https://example.com/module.mjs';
    fn();
</script>

This is es-module import:

import fn from 'https://exmaple.com/module.mjs';
fn();

Site example.com must have CORS enabled (apache config for example):

Header add Access-Control-Allow-Origin "*"

Upvotes: 3

lisonge
lisonge

Reputation: 487

you can use these

Upvotes: 1

Related Questions