I have three files, let it be index.html, module.js, main.js, index.js
INDEX.HTML
....
<body
<script type="module "src="main.js"></script>
<script src="index.js"></script>
</body>
...
MODULE.JS
....
function foo(text){
alert(text)
}
export default foo;
...
MAIN.JS
import foo from "./module.js"
function bar(text){
foo("FOOBAR")
}
INDEX.JS
bar() // not "type="module"
Can I execute bar() from index.js without giving attribute type="module" in html?
>Solution :
That’s not possible. As Ruben mentioned in the comment, you need to import the main.js into your index.js to have access to its bar() function, else you will get a
Uncaught ReferenceError: bar is not defined
error
Checking these questions may be helpful :
how-to-use-code-from-script-with-type-module
use-functions-defined-in-es6-module-directly-in-html