1

when i tried to import a class from one js file to another i got this error

Uncaught SyntaxError: Cannot use import statement outside a module

when i added type="module" to my script tags i got this error

Access to script at 'file link' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

any help on how to fix the error ?

  • You'll need to show how you are writing your `import` statement, and the relative folder structure between your js files. – Heretic Monkey Jul 09 '20 at 16:02
  • 1
    Sounds like you're opening your HTML file directly in the browser... you can fix this error by serving your web files from an HTTP server running on localhost – mlibby Jul 09 '20 at 16:02

3 Answers3

1

this was simply solved by adding type="module" to my script tags and serving my web files from an HTTP server running on localhost instead of opening the html file directly

these answers were helpful :

How to run html file on localhost?

1

First off, you have to include the type="module" attribute in your script tag.

Secondly, when using the import statement, don't leave out the file extension, in this case .js. e.g: import { sqrRoot } from "./math_function.js"

Dharman
  • 30,962
  • 25
  • 85
  • 135
Bravo Stack
  • 111
  • 1
  • 6
0

Just run your application by using your http server. Do not access it directly.

thegreytangent
  • 316
  • 2
  • 12