Does Electron support ES6?


#1

I just started fiddling around with Electron, and was wondering how to use ES6 features, I tried this in main.js, but it didn’t seem to work.

mainWindow.on('closed', () => {
    mainWindow = null;
});

#2

You’ll have to use babel


#3

Woulnd’t it be possible to compile against another io.js version?

https://iojs.org/en/es6.html


#4

I am not sure about accessing ES6 features on electron, because as far as I remember, I was able to use generators just by typing them without using babel. I am not sure about other features, though… What exactly is the error with your snippet above?


#5

This is the error

App threw an error when running [SyntaxError: /Users/USER/www-local/my-app/main.js:38
  mainWindow.on('closed', () => {
                           ^
Unexpected token )]

#6

That page doesn’t mention anything about arrow functions, so I think io.js doesn’t support them yet. You can use this table to see which features are already supported by default in io.js. For the others you’ll have to use babel or another transpiler.


#7

Yea that’s indeed the issue, thanks for pointing it out!


#8

is that code on you main js page, as in the starter that is initially called (app.js), as this cannot be done in ES2015/6 but other files can, i have done some ES2015/6 test as i was wondering as well and alls fine.

Im using gulp, browserify & babelify for my test with vuejs and react js.

my react is not up jet, but hope this helps


#9

It was code in main.js yes. The main reason I like Electron is because I don’t have to worry about transpiling code to work in all browsers :slight_smile: So Babel is no option. But I did figure out that arrow functions just aren’t supported yet :smile:


#10

It’s been almost a year.

Does Electron support ES6 on the main thread?

Node 6 has great ES6 support.


#11

Latest electron is based on Chrome 51 which supports ~98% of ES6.


#12

What about the Node part?

It seems Electron includes Node 6 but this throws an error:

import app from 'app’
import BrowserWindow from ‘browser-window’


#13

ES6 modules are not implemented in either Node 6 or Chrome 51 so Electron also does not support them.


#14

Oh right…

Thanks.


#15

of course electron supports ES6, at least the latest version as support, chromium 51 as 98% of compatibility


#16

It’s possible to use Babel in Electron.

Simplest solution possible: babel-register hook:


this and .babelrc file with presets you want (es2015, react, whatever you need).


#17

As Electron has been upgraded to more recent versions of Node.js and Chromium, you might not need babel anymore. You definitely do not have to install the bloated babel-preset-es2015 anymore, though. You can use babel-preset-electron instead.


#18

you might not need babel anymore.
It depends what features you need.

I have the code which uses ES6 modules and they are still not supported AFAIK.

edit:
but I’ll take a look on this babel-preset-electron. It seems that it removes unnecessary transforms from ES2015 preset:
https://raw.githubusercontent.com/emorikawa/babel-preset-electron/37.8/index.js
So I’ll probably switch to this instead using ES2015 preset.


#19

We’re now in 2018, it doesn’t seem like electron supports import statements yet.

I’m using -r babel-register on the command line but still running into issues when loading a preload script when creating a new BrowserWindow.