"Uncaught SyntaxError: Unexpected token {" while importing
Your syntax is fine if you're using a transpiler, specifically babel is the transpiler most people use these days.
You can specifically enable some es6 functionality (including import and export) in modern browsers using the "module" type in your script <script type=module src="main.js" />
but if you're running locally cross origin request stuff will not cooperate, so you can host your code locally using a web server like express.js
There are many many tutorials available online giving you a rundown on how to begin with web development in the modern age, but here's a list of topics you can research yourself.
node.js
This is for javascript executionexpress.js
This is for creating servers usingnode.js
es6
This is a specification, a newer "version" of ecmascript (javascript), it has new features like import and export and object destructuringbabel
This is a transpiler, something that converts new code (such as es6) into code that can run on older systems (such as es5)webpack
This is a javascript packaging system, used to combine code and execute modules like transpilers
There's a lot to learn, and one of the best places that we as a community can improve. To avoid all of the messy startup stuff you can try a project like https://codesandbox.io/ which aims to make this whole process much easier.
Based on the error message
- you're running Chrome,
- you haven't set
type="module"
in the script tag
I'm trying to find some good documentation regarding module type for script tag, however this is the best I can find so far
So, if your script tag is
<script type="application/javascript">
import {Event} from "event.js";
......
</script>
or even if it is the following (because type is optional)
<script>
import {Event} from "event.js";
......
</script>
change it to
<script type="module">
import {Event} from "event.js";
......
</script>