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 ?

2
  • You'll need to show how you are writing your import statement, and the relative folder structure between your js files. Commented Jul 9, 2020 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 Commented Jul 9, 2020 at 16:02

3 Answers 3

3

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?

Sign up to request clarification or add additional context in comments.

Comments

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"

Comments

0

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

1 Comment

No more local development from file:/// ?.. Times, times...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.