ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import

JavascriptHtmlEcmascript 6ModuleEs6 Modules

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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!

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

It could be that you changed an existing script to a module and forgot to remove the original script tag. This is what happened to me and how I got directed to this page. I had originally had this:

<script src="app.js" defer></script>

Then I changed my main script tag to import it as a module and it was working, but I still received the error. I couldn't figure out how it could be working and throwing an error, but I had forgotten to remove the original script tag.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncdarwinView Question on Stackoverflow
Solution 1 - JavascriptvullnetyyView Answer on Stackoverflow
Solution 2 - JavascriptRoopesh ReddyView Answer on Stackoverflow
Solution 3 - Javascriptt3h2masView Answer on Stackoverflow
Solution 4 - JavascriptabernierView Answer on Stackoverflow
Solution 5 - JavascriptJason GoemaatView Answer on Stackoverflow