简体   繁体   中英

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

you can use these

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM