0

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.

Ahmed N.
  • 15
  • 4
  • Can you please elaborate? Have I not explicitly exported by doing 'export someFunc() {}' and imported it where it was used (in main.js)? Thanks – Ahmed N. May 01 '21 at 02:52
  • 1
    Always declare variables before using them, else you'll run into problems, especially in strict mode – CertainPerformance May 01 '21 at 02:53
  • 1
    Completely neglected let keyword since it usually doesn't cause me problems, it worked once I added it. Thanks – Ahmed N. May 01 '21 at 02:58

0 Answers0