I\'m trying to use async, await from scratch on Babel 6, but I\'m getting regeneratorRuntime is not defined.
.babelrc file
{
\"presets\": [ \"es2
I fixed this error by installing babel-polyfill
npm install babel-polyfill --save
then I imported it in my app entry point
import http from 'http';
import config from 'dotenv';
import 'babel-polyfill';
import { register } from 'babel-core';
import app from '../app';
for testing I included --require babel-polyfill in my test script
"test": "export NODE_ENV=test|| SET NODE_ENV=test&& mocha --compilers
js:babel-core/register --require babel-polyfill server/test/**.js --exit"
For people looking to use the babel-polyfill
version 7^ do this with webpack
ver3^.
Npm install the module npm i -D @babel/polyfill
Then in your webpack
file in your entry
point do this
entry: ['@babel/polyfill', path.resolve(APP_DIR, 'App.js')],
I had a setup
with webpack using presets: ['es2015', 'stage-0']
and mocha that was running tests compiled by webpack.
To make my async/await
in tests work all I had to do is use mocha with the --require babel-polyfill
option:
mocha --require babel-polyfill
To babel7 users and ParcelJS >= 1.10.0 users
npm i @babel/runtime-corejs2
npm i --save-dev @babel/plugin-transform-runtime @babel/core
.babelrc
{
"plugins": [
["@babel/plugin-transform-runtime", {
"corejs": 2
}]
]
}
taken from https://github.com/parcel-bundler/parcel/issues/1762
If you using Gulp + Babel for a frontend you need to use babel-polyfill
npm install babel-polyfill
and then add a script tag to index.html above all other script tags and reference babel-polyfill from node_modules
You're getting an error because async/await use generators, which are an ES2016 feature, not ES2015. One way to fix this is to install the babel preset for ES2016 (npm install --save babel-preset-es2016
) and compile to ES2016 instead of ES2015:
"presets": [
"es2016",
// etc...
]
As the other answers mention, you can also use polyfills (though make sure you load the polyfill first before any other code runs). Alternatively, if you don't want to include all of the polyfill dependencies, you can use the babel-regenerator-runtime or the babel-plugin-transform-runtime.