Babel 6 regeneratorRuntime is not defined

前端 未结 30 2259
暖寄归人
暖寄归人 2020-11-22 03:49

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

.babelrc file

{
    \"presets\": [ \"es2         


        
30条回答
  •  野性不改
    2020-11-22 04:22

    I started getting this error after converting my project into a typescript project. From what I understand, the problem stems from async/await not being recognized.

    For me the error was fixed by adding two things to my setup:

    1. As mentioned above many times, I needed to add babel-polyfill into my webpack entry array:

      ...
      
      entry: ['babel-polyfill', './index.js'],
      
      ...
    2. I needed to update my .babelrc to allow the complilation of async/await into generators:

      {
        "presets": ["es2015"],
        "plugins": ["transform-async-to-generator"]
      }

    DevDependencies:

    I had to install a few things into my devDependencies in my package.json file as well. Namely, I was missing the babel-plugin-transform-async-to-generator, babel-polyfill and the babel-preset-es2015:

     "devDependencies": {
        "babel-loader": "^6.2.2",
        "babel-plugin-transform-async-to-generator": "^6.5.0",
        "babel-polyfill": "^6.5.0",
        "babel-preset-es2015": "^6.5.0",
        "webpack": "^1.12.13"
     }
    

    Full Code Gist:

    I got the code from a really helpful and concise GitHub gist you can find here.

提交回复
热议问题