87

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?

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
cdarwin
  • 3,801
  • 8
  • 38
  • 56
  • ES6 *modules* are not yet supported in the browser. Also you're still loading a script, not a module. – Bergi Jan 18 '17 at 15:02
  • 3
    I still don't understand the difference between a script and a module – cdarwin Jan 18 '17 at 15:08
  • See [here](http://stackoverflow.com/a/39652842/1048572) – Bergi Jan 18 '17 at 17:04
  • 20
    The important part I have come to notice is `````` make sure you add that otherwise you will get that error. I was banging my head on the wall continually doing `````` knowingly that chrome is now said to support ES6 modules without flags, then I noticed that the type attribute was needed to specify to the browser that this is an ES6 module, without which you get that exact error. – Emmanuel Mahuni Sep 13 '17 at 09:17
  • 2
    I am using Chrome 68, I still see this error when we use import * from – Integration Aug 26 '18 at 19:07

4 Answers4

68

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

vullnetyy
  • 1,258
  • 11
  • 14
53

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!

Mir-Ismaili
  • 8,113
  • 2
  • 52
  • 76
Roopesh Reddy
  • 771
  • 1
  • 7
  • 8
  • 33
    That's right... The important part I have come to notice is `````` make sure you add that otherwise you will get that error. I was banging my head on the wall continually doing `````` knowingly that chrome is now said to support ES6 modules without flags, then I noticed that the type attribute was needed to specify to the browser that this is an ES6 module. – Emmanuel Mahuni Sep 13 '17 at 09:13
  • 1
    { "message": "Uncaught SyntaxError: Unexpected token {", "filename": "https://stacksnippets.net/js", "lineno": 24, "colno": 8 } – hoogw Jul 02 '18 at 16:35
  • 4
    I got above error by run your code snippet above, use chrome v67, why? – hoogw Jul 02 '18 at 16:36
  • @hoogw Stackoverflow added that Run code snippet automatically. This code cannot be executed as is. You have to copy the code to index.html and separate .js files as showed above and try in the browser! – Roopesh Reddy Jul 08 '18 at 14:57
  • Thank useful answer. I removed snippet runner for you. (As I saw StackOverflow snippets can't to has more than one `js` source). – Mir-Ismaili Apr 18 '19 at 00:44
  • My chrome Version 90.0.4430.93 Error: Uncaught SyntaxError: Cannot use import statement outside a module While I am just trying to use in my JS file only. @RoopeshReddy can you please help me out here. – Chintan Khetiya Apr 30 '21 at 04:58
25

Unfortunately, modules aren't supported by many browsers right now.

This feature is only just beginning to be implemented in browsers natively at this time. It is implemented in many transpilers, such as TypeScript and Babel, and bundlers such as Rollup and Webpack.

Found on MDN

kleinfreund
  • 5,791
  • 4
  • 25
  • 56
t3h2mas
  • 679
  • 9
  • 11
  • I read this feature was implemented in a Sof question, but theMDN source is actually more reliable. – cdarwin Jan 18 '17 at 15:11
  • 1
    According to the following link Chrome 61 should support import - it doesn't. https://medium.com/dev-channel/es6-modules-in-chrome-canary-m60-ba588dfb8ab7 – Marc Sep 19 '17 at 16:52
  • 4
    You have to add type="module" to your script tag. – smohadjer Jul 22 '18 at 11:24
11

it worked for me adding type="module" to the script importing my mjs:

<script type="module">
import * as module from 'https://rawgit.com/abernier/7ce9df53ac9ec00419634ca3f9e3f772/raw/eec68248454e1343e111f464e666afd722a65fe2/mymod.mjs'

console.log(module.default()) // Prints: Hi from the default export!
</script>

See demo: https://codepen.io/abernier/pen/wExQaa

abernier
  • 22,855
  • 18
  • 73
  • 103