Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
"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 :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

  • Live server extension for visual studio code
  • Node express.js serve static files
  • Github pages publish your project to a github repository and make use of github pages to serve your files
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!