I’m having import and export problems using ES6

Asked

Viewed 336 times

2

I am using Google Chrome as a browser and when I try to run the code, this error appears. And I saw that in the HTML file I can use type="module", however, two other errors appear. The first error is without type="module", and the bottom two are with type="module"

Uncaught Syntaxerror: Unexpected token {

Access to script at 'file://C:/Users/Vagner%20Wentz/Desktop/cursoes6/main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for Protocol schemes: http, data, Chrome, Chrome-Extension, https.

GET file://C:/Users/Vagner%20Wentz/Desktop/cursoes6/main.js net:ERR_FAILED

//funcoes.js
export function soma(a, b) {
    return a + b;
}

//main.js
import { soma } from './funcoes.js';

console.log(soma(1, 2));
  • You’re trying to open directly in the browser or the app is running on some web server?

  • I’m using a webpack server that Diego from rocketseat taught to install

  • Add what is necessary to reproduce your problem in the question, otherwise it is difficult

2 answers

1

Go to your webserver setup in the webpack and add:

headers: {
            'Access-Control-Allow-Origin': '*'
        }
  • Anywhere in the webpack?

  • where you have new WebpackDevServer(...,{ adiciona os headers aqui } );

  • Elaborate a little more the answer explaining why the error and in which case your solution would solve the problem

1


The error is because you are importing main.js together with Bundle.js. main.js contains the uncompleted code, hence the error:

Uncaught Syntaxerror: Unexpected token {

Just remove the main.js.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.