Gabriel
Gabriel

Reputation: 5734

JS import: Uncaught SyntaxError: Unexpected token {

tools.js:

function someFunction() {
  console.log(1 + 1);
}

export { someFunction };

main.js

window.onload = function() {
  import { someFunction } from 'tools';
};

index.html:

<script src="http://localhost:8080/tools.js" type="module"></script>
<script src="http://localhost:8080/main.js "></script>

And yet I get the error on the subject. What am I doing wrong? Thanks.

Upvotes: 1

Views: 1954

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138557

imports always have to be on the global scope:

 import { someFunction } from 'tools.js';

 window.onload = function() {
   //...  
  };

Upvotes: 4

Related Questions