Home > Web Front-end > JS Tutorial > Why Am I Getting \'regeneratorRuntime is not defined\' When Using Async/Await with Babel 6?

Why Am I Getting \'regeneratorRuntime is not defined\' When Using Async/Await with Babel 6?

Susan Sarandon
Release: 2024-12-08 17:38:12
Original
141 people have browsed it

Why Am I Getting

"Babel 6 regeneratorRuntime is not defined" Explained

When implementing async/await in Babel 6, the "regeneratorRuntime is not defined" error commonly arises. To rectify this issue, one must additionally install and include babel-polyfill.

Installing Babel Dependencies

To use async/await, install these dependencies:

npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader
Copy after login

Updating package.json

Add the following to "devDependencies":

"babel-polyfill": "^6.0.16"
Copy after login

Maintaining .babelrc

Keep the settings as is:

{
  "presets": [ "es2015", "stage-0" ]
}
Copy after login

Setting Up Async/await**

Enable async/await in your code:

"use strict";

export default async function foo() {
  var s = await bar();
  console.log(s);
}

function bar() {
  return "bar";
}
Copy after login

Loading Required Packages

In your startup file, include:

require("babel-core/register");
require("babel-polyfill");
Copy after login

Webpack Configuration

If using webpack, ensure babel-polyfill is the first entry:

module.exports = {
  entry: ['babel-polyfill', './test.js'],

  output: {
    filename: 'bundle.js'       
  },

  module: {
    loaders: [
      { test: /\.jsx?$/, loader: 'babel', }
    ]
  }
};
Copy after login

Testing with Babel

Run tests with Babel using:

mocha --compilers js:babel-core/register --require babel-polyfill
Copy after login

The above is the detailed content of Why Am I Getting \'regeneratorRuntime is not defined\' When Using Async/Await with Babel 6?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template