I've been researching this for weeks and have spent hours on this the last two days, but I cannot find a proper answer.
I've read several times that the latest Chrome versions support ECMAScript 6 and that they support import/export statements.
So why is my below script returning an error?
Error:
index.js:1 Uncaught SyntaxError: Unexpected token {
My code:
// Player.js
export class Player {
constructor() {
this.name = 'TestPlayer2';
}
}
// index.js
import { Player } from './Player.js';
let MyPlayer = new Player();
window.alert(MyPlayer.name);
// index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Import Test</title>
</head>
<body>
Test
<script type="text/javascript" src="index.js"></script>
</body>
</html>
Do recent browsers not support these import and export statements?
Did they ever?
Why is every EMCAScript 6 topic/blog/post saying recent browsers support this?
Is there another import/export format that browsers do support?
I don't mind having to add the source Player.js source file in my index.html, but I also need the Player.js file on my node.js server where I'll have to use import/export.
Edit: I've rewritten the question after people suggested using tools like webpack. I got this working with webpack, but I'd like to understand why this is not working as is, since I've read everywhere that browsers do support this.
I probably will be using webpack to compile my files into one big file.
I'm now just interested in knowing why and how this (doesn't) work.