Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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.

like image 706
stevenlacerda Avatar asked Oct 30 '17 21:10

stevenlacerda


People also ask

How to solve the regeneratorruntime problem with async function?

So, to solve the regeneratorRuntime problem, do this: require import 'regenerator-runtime/runtime' at the top of the file that you're using async function I didn't edit webpack, because I'm using Parcel, but just importing regenerator-runtime/runtime at the top of the file solved the problem. wow! It, weirdly enough...worked!

How do I fix the async/await error in Babel?

This error is caused when async/await functions are used without the proper Babel plugins. To install @babel/plugin-transform-runtime by typing in the command line, npm install --save-dev @babel/plugin-transform-runtime Then Add the plugin to pakage.json file in plugins array.

How to install @Babel/plugin-transform-runtime without babelrc?

How we can do it without .babelrc (just using webpack config file)?. This error is caused when async/await functions are used without the proper Babel plugins. To install @babel/plugin-transform-runtime by typing in the command line,


2 Answers

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.

like image 170
stevenlacerda Avatar answered Oct 21 '22 04:10

stevenlacerda


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

like image 36
kakamg0 Avatar answered Oct 21 '22 03:10

kakamg0