Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

javascript - 未定义Babel 6 regeneratorRuntime(Babel 6 regeneratorRuntime is not defined)

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

(我正在尝试使用异步,在Babel 6上从头开始,但是我得到的regeneratorRuntime尚未定义。)

.babelrc file

(.babelrc文件)

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

package.json file

(package.json文件)

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

.js file

(.js文件)

"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?

(有什么想法我做错了吗?)

  ask by BrunoLM translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

babel-polyfill is required.

(babel-polyfill 。)

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

(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

(.babelrc)

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

.js with async/await (sample code)

(带有async / await的.js(示例代码))

"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 value of your entry array in your webpack configuration file (usually webpack.config.js ), as per @Cemen comment:

(如果您使用的是webpack ,则需要按照@Cemen注释 ,将其作为entry数组的第一个值放入webpack配置文件(通常是webpack.config.js )中:)

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:

(如果要使用babel运行测试,请使用:)

mocha --compilers js:babel-core/register --require babel-polyfill

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...