5

I have the latest Chrome version (45 and also Chrome Canary which is in version 47), both with the Experimental JavaScript flag enabled. I want to use ECMAScript 6, but it doesn’t work. I don’t know why. Is there any trick or other flag that must be enabled, too?

Every reserved word of ECMAScript 6 (like import, class, or whatever) throws an “Uncaught SyntaxError: Unexpected reserved word” in Chrome 45 and an “Uncaught SyntaxError: Unexpected token import error in Chrome Canary.

I asked this a few months ago without getting any answer but a “possible duplicate” of Using ECMAScript 6, but it does not solve my problem.

I want to use modules, since I like the ECMAScript modules more than using require from CommonJS. And I also like the syntax sugar of classes — the code looks better.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Emilio Grisolía
  • 1,183
  • 1
  • 9
  • 16
  • 3
    Some (not all) ES6 features are supported in the latest version of Chrome. Please indicate exactly which version of Chrome you are running and exactly how you thought you enabled these features and exactly which ES6 features you find are not enabled. You need to be much more specific in your questions. Create a reproducible scenario. – jfriend00 Sep 27 '15 at 15:50
  • You are right, i add more info :) – Emilio Grisolía Sep 27 '15 at 16:03

3 Answers3

8

Modules are not yet natively supported in any browser. You will need to use a transpiler such as Traceur or Babel. Take a look at one of the following to help you get started:

As for classes, you may be able to use these natively without having to go through a transpiler. You can check the compatibility table here to see which browsers support classes natively today:

https://kangax.github.io/compat-table/es6/

As of right now, you can see that the majority of browsers do not yet support classes natively. However, if you are using Babel or Traceur, that shouldn't be a concern.

Lukas S.
  • 5,698
  • 5
  • 35
  • 50
  • 1
    This answer is not out of date, see: https://medium.com/dev-channel/es6-modules-in-chrome-canary-m60-ba588dfb8ab7 – GrayedFox Jul 02 '17 at 15:35
8

Add type="module" to the script tag. That should solve your problem.

stackoverflow
  • 1,303
  • 2
  • 21
  • 36
3

Here are a couple feature lists that tell you what features work in Chrome 45.

Chrome Feature Status: https://www.chromestatus.com/features

ES6 compatibility matrix: https://kangax.github.io/compat-table/es6/

What you will find is that many features such as the class sugar syntax work fine in Chrome 45, but require strict mode in order to be enabled.

For example, if you run this jsFiddle that uses class in Chrome 45 or greater, it will work: http://jsfiddle.net/jfriend00/xd56k8n3/. If you run it outside of strict mode, it reports Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode.

Modules do not look like they yet have support in any browser.


FYI, one common way to write code now in ES6 is to use a transpiler like Babel or Traceur that you feed ES6 code into and it converts it to ES5-compatible code that runs in current browsers. You get to write in ES6, but have compatibility with current browsers.

jfriend00
  • 683,504
  • 96
  • 985
  • 979