1

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.

1
  • 1
    You cannot use import statements outside the top lines of your file. Commented Nov 4, 2018 at 21:21

1 Answer 1

4

imports always have to be on the global scope:

 import { someFunction } from 'tools.js';

 window.onload = function() {
   //...  
  };
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Jonas, but even if my main.js file has a single line with the import statement, I still get the error.
@GabrielA.Zorrilla it also needs to be type="module"
@PatrickRoberts the main.js right? Because did that and solved the problem. I also had to specify tools.js as the module name, not just tools. Thanks. This will be very hard because both, you and Jonas have answered!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.