Большинство браузеров поддерживают импорт, поэтому должно работать следующее:
/**
* Adder.mjs
*/
class Adder {
add(n1, n2) {
return n1 + n2;
}
}
export default Adder;
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Import Test</title>
<script type="module">
import Adder from "./Adder.mjs";
var adder = new Adder();
var result = adder.add(1, 1);
console.log("Result: " + result.toString());
</script>
</head>
<body>
</body>
</html>
Однако это приводит к следующей ошибке (консоль отладки Chromium):
Access to script at 'file:///home/gso/eclipse-workspace/Test/Adder.js' from origin 'null' has been blocked by CORS policy: The response is invalid.
test.html:7 GET file:///home/gso/eclipse-workspace/Test/Adder.js net::ERR_FAILED