Home > Net >  JavaScript: absolute URL as es-module address in imports
JavaScript: absolute URL as es-module address in imports

Time:10-13

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:'

CodePudding user response:

you can use these

CodePudding user response:

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 "*"
  • Related