7
votes

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:

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

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

Please let me know where I'm going wrong.

2

2 Answers

4
votes

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:

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:

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

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

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

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

1
votes

Async/await is not part of es2015 preset, you should either use es2017 or use transform-async-to-generator with es2015