1

i wanted to move part of my code into JS module, but when I did that i get:

Uncaught SyntaxError: Unexpected token { on line 3 of script.js

The script.js starts like this:

'use strict'

import {MyClass} from "./module.js";

The module.js starts like this:

'use strict';

export class MyClass {

Both files are imported in index.html at the end of body:

<script src="js/module.js" type="module"></script>
<script src="js/script.js"></script>

When I change reference to script.js to type="module", the error is not thrown anymore, but I cannot use any of script.js's functions in onload etc.

Gomi
  • 632
  • 1
  • 7
  • 23

1 Answers1

0

Some browsers support ES6 modules as long as you use type="module" in your imports. So you need to make it like this:

<script src="js/script.js" type="module"></script>

Now you said that you cannot use functions from script.js when you do this. could you be more specific ? (by sharing code and error may be)

Ichraf
  • 345
  • 1
  • 7
  • In **script.js**, there is function *processFile()* which i use to process file input. I use: And it throws me function not found. – Gomi Jun 06 '19 at 06:50
  • can you share your html and typescript code ? it could help see better :) ! – Ichraf Jun 06 '19 at 07:28
  • Sorry I know this is not the answer, but I am having the same issue, this seems to be what is happening but I cannot seem to get an onload event to take a module funtion either. https://stackoverflow.com/questions/53630310/use-functions-defined-in-es6-module-directly-in-html – John Dowling Jun 06 '19 at 09:18