Home >Web Front-end >JS Tutorial >How to Fix the \'regeneratorRuntime is not defined\' Error in Babel 6 with Async/Await?
Resolving "regeneratorRuntime is not defined" Error with Babel 6 Async/Await
When attempting to implement async/await functionality in Babel 6, developers may encounter the "regeneratorRuntime is not defined" error. This is because, unlike in later versions of Babel, async/await support requires the babel-polyfill package in Babel 6.
Solution:
Install babel-polyfill:
npm i -D babel-polyfill
Update package.json:
"devDependencies": { "babel-polyfill": "^6.0.16", }
Modify webpack configuration (if applicable):
In webpack.config.js, make sure babel-polyfill is the first entry in the entry array:
module.exports = { entry: ['babel-polyfill', './test.js'], ... };
Setup babel-polyfill and babel-core in the startup file:
require("babel-core/register"); require("babel-polyfill");
Example Code:
"use strict"; export default async function foo() { var s = await bar(); console.log(s); } function bar() { return "bar"; }
Running Tests with Babel:
When running tests with Babel, use the following command:
mocha --compilers js:babel-core/register --require babel-polyfill
The above is the detailed content of How to Fix the \'regeneratorRuntime is not defined\' Error in Babel 6 with Async/Await?. For more information, please follow other related articles on the PHP Chinese website!