Babel 7 - ReferenceError: regeneratorRuntime is not defined

喜夏-厌秋 提交于 2019-12-03 09:51:15
kojow7

Update Answer:

If you are using Babel 7.4.0 or newer, then @babel/polyfill has been deprecated. Instead, you will want to use the following:

import "core-js/stable";
import "regenerator-runtime/runtime";

Add these using yarn:

yarn add core-js
yarn add regenerator-runtime

Original Answer:

I just encountered this problem and came across the following solution:

In package.json I had @babel/polyfill as a dependency. However, in my index.js (My main js file) I had neglected to place the following line at the the top:

import '@babel/polyfill'

Once I imported it, everything worked fine.

I did not need to install babel-runtime as other answers are suggesting.

There is already a very good answer here (originally posted on the Babel6 question) which I will just translate to Yarn. Basically you need babel runtime (NOT as a dev dependency) and the plugin transform-runtime

yarn add @babel/runtime 
yarn add -D @babel/plugin-transform-runtime

And, in .babelrc, add:

{
    "presets": ["@babel/preset-env"],
    "plugins": [
        ["@babel/transform-runtime"]
    ]
}

You will need to have the regeneratorRuntime.

Install this two packages - babel-plugin-transform-regenerator and babel-polyfill

Add the following Babel configuration via .babelrc

{
  "plugins": ["transform-regenerator"]
}

React.js Users

If this issue faced you while using react (specifically while trying to use Async/Wait), then Valentino Gagliardi provided a detailed approach on his blog regarding how to address this issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!