I have a html file and two JavaScript files, one is the main, and the other contains a function that I want to import into main.
For example -
main.js:
import {someFunc} from "./function.js"
randomVariable = 50;
someFunc()
exporting.js:
export someFunc() {
return 'foo';
}
HTML Tags (in < head >):
< script src="./main.js" type="module" defer >< /script >
< script src="./exporting.js" type="module" defer >< /script >
When I import the function into main.js, for some reason I get this error:
Uncaught ReferenceError: randomVariable is not defined
Which means that the variable I defined in main becomes undefined. When I merge the two files back together without importing / exporting anything, everything works fine.