some_file.js
console.log("!")
function hello(){
console.log("Hello!")
}
index.html
<script src="some_file.js" type="module"></script>
<script>
hello()
</script>
When hosting and looking at index.html, I recieve a ReferenceError telling me that hello is not found. Following the advice to this thread other similar ones, I've placed something like window.hello = hello in my javascript file to no avail. I've also attempted exporting the function (which I think I'd only need to do if I were importing it in another javascript file).
Why am I getting a ReferenceError? The script is certainly being loaded, because I see the ! print out in the console, but the function isn't being found.
2 Answers 2
Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules You have to export the function, then within a script tag with type module, import the module you want to use.
some_file.js
console.log("!")
export function hello(){
console.log("Hello!")
}
index.html
<script type='module'>
import {hello} from "./some_file.js"
hello()
</script>
3 Comments
Try this:
index.html
<script src="some_file.js" type="module">
hello()
</script>
1 Comment
Expected expression, got < comes up)
export function hello() ...type="module"in my HTML