繁体   English   中英

与babel一起使用async / await时,没有定义regeneratorRuntime错误

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

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

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:

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“}

我还需要(“babel-core / register”)在我的entry.jsx文件的顶部。

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

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

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)在我的entry.jsx我有:

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

在webpack中,我的require('babel-polyfill')语句被悬挂在上面,因此我将其更改为:

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

瞧,问题消失了。 哦,并确保你有最新的babel核心和babel-loader。

Async / await不是es2015预设的一部分,您应该使用es2017或使用变换 - 异步 - 生成器与es2015

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM