I'm trying to use async, await from scratch on Babel 6, but I'm getting regeneratorRuntime is not defined.
.babelrc file
{
"presets": [ "es2015", "stage-0" ]
}
package.json file
"devDependencies": {
"babel-core": "^6.0.20",
"babel-preset-es2015": "^6.0.15",
"babel-preset-stage-0": "^6.0.15"
}
.js file
"use strict";
async function foo() {
await bar();
}
function bar() { }
exports.default = foo;
Using it normally without the async/await works just fine. Any ideas what I'm doing wrong?
1 - Install babel-plugin-transform-async-to-module-method, babel-polyfil, bluebird , babel-preset-es2015, babel-core :
2 - Add in your js babel polyfill:
import 'babel-polyfill';
3 - Add plugin in your .babelrc:
Source : http://babeljs.io/docs/plugins/transform-async-to-module-method/
The targeted browsers I need to support already support async/await, but when writing mocha tests, without the proper setting I still got this error.
Most of the articles I googled are outdated, including the accepted answer and high voted answers here, i.e. you don't need
polyfill
,babel-regenerator-runtime
,babel-plugin-transform-runtime
. etc. if your target browser(s) already supports async/await (of course if not you need polyfill)I don't want to use
webpack
either.Tyler Long's answer is actually on the right track since he suggested
babel-preset-env
(but I omitted it first as he mentioned polifill at the beginning). I still got theReferenceError: regeneratorRuntime is not defined
at the first then I realized it was because I didn't set the target. After setting the target for node I fix the regeneratorRuntime error:Babel 7 Users
I had some trouble getting around this since most information was for prior babel versions. For Babel 7, install these two dependencies:
And, in .babelrc, add:
To babel7 users and ParcelJS >= 1.10.0 users
.babelrc
taken from https://github.com/parcel-bundler/parcel/issues/1762
I had this problem in Chrome. Similar to RienNeVaPlu͢s’s answer, this solved it for me:
Then in my code:
Happy to avoid the extra 200 kB from
babel-polyfill
.I have async await working with webpack/babel build:
.babelrc: