0

How can I import a class from another file within a file imported by a module in electron (or is this just the completly wrong way and you shouldn't do nested imports; and if, what you should do instead)?

If I try, I get the following error:
Uncaught SyntaxError: Unexpected identifier

That is the example code:

//index.html
...
<script src="script1.js" type="module"></script>
...

//script1.js
import Class1 from './script2.js';
const class1 = new Class1();

//script2.js
class Class1 {
  constructor() {
    //doStuff
    import Class2 from './script3.js';
    const class2 = new Class2();
  }
}
export default Class1;

//script3.js
class Class2 {
  constructor() {
    //doStuff
    console.log("Class2");
  }
}
export default Class2;

  • 1
    I think this should answer your question. https://stackoverflow.com/questions/34203325/why-must-export-import-declarations-be-on-top-level-in-es2015 – Anurag Dec 17 '20 at 11:58

1 Answers1

0

Thanks a lot Anurag! To make the solution more prominent, I post the solution here again:

The import statement has to be on top level of the file and not inside the class.

Read this question and the answer for details about the reasons: Why must export/import declarations be on top level in es2015?