Electron and Babel 6 async / await throw unexpected token

时间:2016-04-21 22:27:29

标签: javascript node.js babeljs electron ecmascript-next

I want to use async / await feature from ES7 in my Electron app, but it seems to be not working. It gives me

Syntax error: unexpected token function

after command npm start..

Electron: v0.37.6 Node: v5.11.0 stable Windows 10 x64

main.js

'use strict';

require("babel-core/register");
require("babel-polyfill");

(async function() {
  await console.log("test");
})()

package.json (snipped)

"devDependencies": {
  "babel": "^6.5.2",
  "babel-cli": "^6.7.7",
  "babel-core": "^6.7.7",
  "babel-eslint": "^6.0.3",
  "babel-plugin-syntax-async-functions": "^6.5.0",
  "babel-plugin-transform-async-to-generator": "^6.7.4",
  "babel-plugin-transform-regenerator": "^6.6.5",
  "babel-polyfill": "^6.7.4",
  "babel-preset-es2015": "^6.6.0",
  "babel-preset-stage-3": "^6.5.0",
  "electron-debug": "^0.6.0",
  "electron-prebuilt": "^0.37.0",
  "eslint": "^2.8.0"
}

.babelrc

{
  "presets": ["es2015", "stage-3"],
  "plugins": ["transform-async-to-generator", "syntax-async-functions", "transform-regenerator"]
}

Have you any idea what's missing in my conf etc. please?

Edit

I've also tried to add some import after require() but it ends with

Unexpected token import

It seems to like Babe is not loaded at all..

2 个答案:

答案 0 :(得分:2)

最后,我通过两个简单的步骤解决了这个问题:

  1. 将我的Babel内容放在单独的文件中
  2. <强> index.js

    'use strict';
    
    require('babel-core/register');
    require("babel-polyfill");
    require("./src/main");
    
    1. 更新了package.json以在npm start
    2. 上执行它

      <强>的package.json

      "scripts": {
        "start": "electron index.js"
      },
      

答案 1 :(得分:-2)

我遇到了同样的问题。 以下是我解决问题的方法:http://masnun.com/2015/11/11/using-es7-asyncawait-today-with-babel.html

修改

await应与promises

一起使用