Uncaught ReferenceError: regeneratorRuntime is not defined – Two Solutions

kevin.Zhu 发布于:2017-9-1 14:33 分类:Nodejs  有 16 人浏览,获得评论 0 条  

http://bird.so/search?q=Uncaught+ReferenceError%3A+regeneratorRuntime+is+not+defined+%E2%80%93+Two+Solutions


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