Skip to content
Advertisement

How to allow async functions in React + Babel?

I have a Typescript/React app, that can perform async function with a then/catch promise, but not with async/await/try/catch.

The error is: Uncaught ReferenceError: regeneratorRuntime is not defined .

The error seems to come from Babel. Here is my config:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-typescript",
    "@babel/preset-react"
  ],
  "plugins": ["babel-plugin-styled-components"]
}

How to fix this?

Advertisement

Answer

You can find your solution at here

If I summarise then you have to install a babel plugin named plugin-transform-runtime and need to configure the .babelrc settings.

npm install @babel/plugin-transform-runtime --save-dev
npm install @babel/runtime

After installing these two go to the .babelrc file and add these plugins.

"plugins": [
    ["@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ]
  ],
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement