I've tried to read up on JavaScript modules, but I'm still not clear on one thing: assume the following minimal HTML example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Test</title>
<script type="module">
export function hello() {
console.log("hello");
}
</script>
</head>
<body>
</body>
</html>
Let's say I load this in my browser via file:///C:/tmp/test.html
- it is the file://
protocol, yes, but there is no .js file being loaded, as the script is embedded, so CORS should not be a problem (and I don't get a Console JavaScript error either).
So let's say, I load this page in Firefox 115, and I open the Console/JavaScript Debugger. No "hello" is accessible by default:
>> hello
Uncaught ReferenceError: hello is not defined
<anonymous> debugger eval code:1
... and I cannot "import" or "require":
>> import { hello }
Uncaught SyntaxError: import declarations may only appear at top level of a module
>> require("hello")
Uncaught ReferenceError: require is not defined
<anonymous> debugger eval code:1
(maybe require
needs a library, but as you can tell, I'm not really a JavaScript guru)
So - is there anything I can do with my <script type="module">
from "normal" JavaScript (that is, either <script>
which is not type="module"
- or direct commands in JavaScript Debugger/Console), without explicitly saying window.hello = hello;
at the end of the module code?