javascript 将 async/await 与 babel 一起使用时,regeneratorRuntime 未定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47024780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:07:56  来源:igfitidea点击:

regeneratorRuntime is not defined error when using async/await with babel

javascript

提问by stevenlacerda

I am using async/await code and I'm receiving a "regeneratorRuntime is not defined error". I have tried several solutions from stack overflow, but I can't get any of them to work. Here's my configuration:

我正在使用 async/await 代码,但收到“regeneratorRuntime 未定义错误”。我已经尝试了几种堆栈溢出的解决方案,但我无法让它们中的任何一个工作。这是我的配置:

webpack.config.js:

webpack.config.js:

module.exports = {    
    entry: ['babel-polyfill', './client/libs/compileTemplate/entry.jsx', './client/libs/compileTemplate/loginEntry.jsx'],
    output: {
        path: '/dist',
        publicPath: '/assets',
        filename: '[name].js'
    },
    plugins: plugins,
    externals: {},
    module: {
        loaders: require('./webpack.config.loaders')
    },

...

...

webpack.config.loaders.js:

webpack.config.loaders.js:

module.exports = [
  {
    test: /\.jsx$/,
    loader: 'babel',
    query: {
      presets: ['es2015', 'stage-0', 'react'],
    }
  },
];

package.json: "devDependencies": { "babel-core": "^6.7.5", "babel-loader": "^6.2.4", "babel-polyfill": "^6.26.0", "babel-preset-es2015": "6.16.0", "babel-preset-react": "6.16.0", "babel-preset-stage-0": "^6.24.1", "json-loader": "^0.5.4", "less-loader": "2.2.3" }

package.json: "devDependencies": { "babel-core": "^6.7.5", "babel-loader": "^6.2.4", "babel-polyfill": "^6.26.0", "babel -preset-es2015": "6.16.0", "babel-preset-react": "6.16.0", "babel-preset-stage-0": "^6.24.1", "json-loader": " ^0.5.4", "less-loader": "2.2.3" }

I have also require("babel-core/register") at the top of my entry.jsx file.

我的 entry.jsx 文件顶部也有 require("babel-core/register") 。

Please let me know where I'm going wrong.

请让我知道我哪里出错了。

采纳答案by stevenlacerda

Okay, so several things resolved my problem.

好的,有几件事解决了我的问题。

1) npm i -S babel-preset-env babel-polyfill and in my webpack.config.js I added 'env', which takes care of es2015, es2016, and es2017 in one fell swoop:

1) npm i -S babel-preset-env babel-polyfill 并在我的 webpack.config.js 中添加了“env”,它一举处理了 es2015、es2016 和 es2017:

module: {
    loaders: [
       { test: /\.jsx$/, loader: 'babel-loader', query: { presets: ['env', 'react'] } },
    ]
},


entry: [ './client/libs/compileTemplate/entry.jsx', './client/libs/compileTemplate/loginEntry.jsx'],

2) in my entry.jsx I had:

2)在我的 entry.jsx 我有:

async function createSessionInfo() { // stuff here };

which was being hoisted above my require('babel-polyfill') statement in webpack, so I changed it to:

它被提升到 webpack 中我的 require('babel-polyfill') 语句之上,所以我将其更改为:

require('babel-polyfill');
const createSessionInfo = async function() {

And voila, problem gone. Oh, and make sure you have the latest babel-core and babel-loader.

瞧,问题就解决了。哦,确保你有最新的 babel-core 和 babel-loader。

回答by kakamg0

Async/await is not part of es2015 preset, you should either use es2017or use transform-async-to-generatorwith es2015

Async/await 不是 es2015 预设的一部分,你应该使用es2017或使用transform-async-to-generator和 es2015