I'm writing a vanilla JS project and I'm encountering an issue.
test1.js:
export default foo;
function foo() {
const something = "a";
}
test2.js:
import foo from test1.js;
foo();
console.log(something);
But when I try to run it in my browser (Firefox), I get Uncaught SyntaxError: export declarations may only appear at top level of a module.
Can anyone help? Thanks a lot!
Edit: Thanks a lot!
you can't call a variable which is inside function. Instead of that you can write :
export default foo;
function foo() {
const something = "a";
console.log(something)
}
then :
import foo from 'test1.js';
foo();
and in your HTML template : <script src="test2.js" type="module"></script>