I'm new to ES6 (ECMAScript 6), and I'd like to use its module system in the browser. I read ES6 is supported by Firefox and Chrome, but I'm getting the following error using export
Uncaught SyntaxError: Unexpected token import
I have a test.html file
<html> <script src="test.js"></script> <body> </body> </html>
and a test.js file
'use strict'; class Test { static hello() { console.log("hello world"); } } export Test;
Why?
Many modern browsers now support ES6 modules. As long as you import your scripts (including the entrypoint to your application) using <script type="module" src="...">
it will work.
Take a look at caniuse.com for more details: https://caniuse.com/#feat=es6-module
You can try ES6 Modules in Google Chrome Beta (61) / Chrome Canary.
Reference Implementation of ToDo MVC by Paul Irish - https://paulirish.github.io/es-modules-todomvc/
I've basic demo -
//app.js import {sum} from './calc.js' console.log(sum(2,3));
//calc.js let sum = (a,b) => { return a + b; } export {sum};
<html> <head> <meta charset="utf-8" /> </head> <body> <h1>ES6</h1> <script src="app.js" type="module"></script> </body> </html>
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With