Jurek O.
Jurek O.

Reputation: 33

"Cannot use import statement outside a module" when importing a self made class

I looked at several other questions and tried a lot around but i cant find the problem.

Those are my imports from the HTML file:

<script src="./script/Graph.js" type="module"></script>
<script src="./script/Main.js"></script>

Graph.js:

export default class Graph {
  constructor() {
      this.vertices = [];
      this.edges = [];
      this.numberOfEdges = [];       
  }
  ...followed by several functions
}

Main.js:

import Graph from "./Graph.js";

Iam thankful for any tip :)

Upvotes: 0

Views: 1115

Answers (1)

AnanthDev
AnanthDev

Reputation: 1818

To use import - export in your files you need to specify the type of script file as module

so the corrected code would be

<script src="./script/Graph.js" type="module"></script>
<script src="./script/Main.js" type="module"></script>

Read more on Modules

Extra:

You might come across CORS (cross origin resource sharing policy) while using import-export, to overcome this you could use one of the following

Upvotes: 1

Related Questions