Skip to content Skip to sidebar Skip to footer

In Browser Js Code That Imports From Es6 Module Is Not Executed

I have an ES6 module (mymodule) and HTML page that has JS code that must be executed when a user opens the page. This is my module: //mymodule.js class TestClass { getString()

Solution 1:

<scriptsrc="./mymodule.js"type="module"></script>

The above is not needed. You can remove it.

import {TestClass} from"./mymodule";

Browsers don't do file extension resolution the way Node.js does. (They can't, because they deal in URLs and not file systems, so there is no way to get a list of files in a directory).

You need to be explicit with the URL:

import {TestClass} from"./mymodule.js";

NB: You also need to load page.html over HTTP and not from the local file system.

Solution 2:

You can export your class to default keyword.

classTestClass {
    getString() {
        return"TestString";
    }
}
exportdefault {TestClass}

Post a Comment for "In Browser Js Code That Imports From Es6 Module Is Not Executed"