Home >Web Front-end >JS Tutorial >Why Am I Getting \'regeneratorRuntime is not defined\' When Using Async/Await with Babel 6?
"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
Updating package.json
Add the following to "devDependencies":
"babel-polyfill": "^6.0.16"
Maintaining .babelrc
Keep the settings as is:
{ "presets": [ "es2015", "stage-0" ] }
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"; }
Loading Required Packages
In your startup file, include:
require("babel-core/register"); require("babel-polyfill");
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', } ] } };
Testing with Babel
Run tests with Babel using:
mocha --compilers js:babel-core/register --require babel-polyfill
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!