MAO-EYE 2015-11-04 16:58 采纳率: 100%
浏览 564
已采纳

未定义 Babel 6 regeneratorRuntime

I'm trying to use async, await from scratch on Babel 6, but I'm getting regeneratorRuntime is not defined.

.babelrc file

{
    "presets": [ "es2015", "stage-0" ]
}

package.json file

"devDependencies": {
    "babel-core": "^6.0.20",
    "babel-preset-es2015": "^6.0.15",
    "babel-preset-stage-0": "^6.0.15"
}

.js file

"use strict";
async function foo() {
  await bar();
}
function bar() { }
exports.default = foo;

Using it normally without the async/await works just fine. Any ideas what I'm doing wrong?

转载于:https://stackoverflow.com/questions/33527653/babel-6-regeneratorruntime-is-not-defined

  • 写回答

7条回答 默认 最新

  • 撒拉嘿哟木头 2015-11-04 17:10
    关注

    babel-polyfill is required. You must also install it in order to get async/await working.

    npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader
    

    package.json

    "devDependencies": {
      "babel-core": "^6.0.20",
      "babel-polyfill": "^6.0.16",
      "babel-preset-es2015": "^6.0.15",
      "babel-preset-stage-0": "^6.0.15"
    }
    

    .babelrc

    {
      "presets": [ "es2015", "stage-0" ]
    }
    

    .js with async/await (sample code)

    "use strict";
    
    export default async function foo() {
      var s = await bar();
      console.log(s);
    }
    
    function bar() {
      return "bar";
    }
    

    In the startup file

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

    If you are using webpack you need to put it as the first entry as per @Cemen comment:

    module.exports = {
      entry: ['babel-polyfill', './test.js'],
    
      output: {
        filename: 'bundle.js'       
      },
    
      module: {
        loaders: [
          { test: /\.jsx?$/, loader: 'babel', }
        ]
      }
    };
    

    If you want to run tests with babel then use:

    mocha --compilers js:babel-core/register --require babel-polyfill
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?