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?
If using
babel-preset-stage-2
then just have to start the script with--require babel-polyfill
.In my case this error was thrown by
Mocha
tests.Following fixed the issue
mocha \"server/tests/**/*.test.js\" --compilers js:babel-register --require babel-polyfill
In a scenario where a custom
babelHelpers.js
file is created usingbabel.buildExternalHelpers()
withbabel-plugin-external-helpsers
I figured the least costly solution for the client is to prepend theregenerator-runtime/runtime.js
to the output instead of all polyfills.This solution comes down to about 20 KB instead of ~230 KB when including
babel-polyfill
.Update
It works if you set the target to Chrome. But it might not work for other targets, please refer to: https://github.com/babel/babel-preset-env/issues/112
So this answer is NOT quite proper for the original question. I will keep it here as a reference to
babel-preset-env
.A simple solution is to add
import 'babel-polyfill'
at the beginning of your code.If you use webpack, a quick solution is to add
babel-polyfill
as shown below:I believe I've found the latest best practice.
Check this project: https://github.com/babel/babel-preset-env
Use the following as your babel configuration:
Then your app should be good to go in the last 2 versions of Chrome browser.
You can also set Node as the targets or fine-tune the browsers list according to https://github.com/ai/browserslist
Tell me what, don't tell me how.
I really like
babel-preset-env
's philosophy: tell me which environment you want to support, do NOT tell me how to support them. It's the beauty of declarative programming.I've tested
async
await
and they DO work. I don't know how they work and I really don't want to know. I want to spend my time on my own code and my business logic instead. Thanks tobabel-preset-env
, it liberates me from the Babel configuration hell.Most of these answers recommend solutions for dealing with this error using WebPack. But in case anyone is using RollUp (like I am), here is what finally worked for me (just a heads-up and bundling this polyfill ads about 10k tot he output size):
.babelrc
rollup.config.js
babel-regenerator-runtime
is now deprecated, instead one should useregenerator-runtime
.To use the runtime generator with
webpack
andbabel
v7:install
regenerator-runtime
:And then add within webpack configuration :
babel-polyfill
is required. You must also install it in order to get async/await working.package.json
.babelrc
.js with async/await (sample code)
In the startup file
If you are using webpack you need to put it as the first entry as per @Cemen comment:
If you want to run tests with babel then use: