Babel 6 Regeneratorruntime Is Not Defined

Babel 6 regeneratorRuntime is not defined

babel-polyfill (deprecated as of Babel 7.4) is required. You must also install it in order to get async/await working.

npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader

package.json

"devDependencies": {
"babel-core": "^6.0.20",
"babel-polyfill": "^6.0.16",
"babel-preset-es2015": "^6.0.15",
"babel-preset-stage-0": "^6.0.15"
}

.babelrc

{
"presets": [ "es2015", "stage-0" ]
}

.js with async/await (sample code)

"use strict";

export default async function foo() {
var s = await bar();
console.log(s);
}

function bar() {
return "bar";
}

In the startup file

require("babel-core/register");
require("babel-polyfill");

If you are using webpack you need to put it as the first value of your entry array in your webpack configuration file (usually webpack.config.js), as per @Cemen comment:

module.exports = {
entry: ['babel-polyfill', './test.js'],

output: {
filename: 'bundle.js'
},

module: {
loaders: [
{ test: /\.jsx?$/, loader: 'babel', }
]
}
};

If you want to run tests with babel then use:

mocha --compilers js:babel-core/register --require babel-polyfill

Babel 6 regeneratorRuntime is not defined

babel-polyfill (deprecated as of Babel 7.4) is required. You must also install it in order to get async/await working.

npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader

package.json

"devDependencies": {
"babel-core": "^6.0.20",
"babel-polyfill": "^6.0.16",
"babel-preset-es2015": "^6.0.15",
"babel-preset-stage-0": "^6.0.15"
}

.babelrc

{
"presets": [ "es2015", "stage-0" ]
}

.js with async/await (sample code)

"use strict";

export default async function foo() {
var s = await bar();
console.log(s);
}

function bar() {
return "bar";
}

In the startup file

require("babel-core/register");
require("babel-polyfill");

If you are using webpack you need to put it as the first value of your entry array in your webpack configuration file (usually webpack.config.js), as per @Cemen comment:

module.exports = {
entry: ['babel-polyfill', './test.js'],

output: {
filename: 'bundle.js'
},

module: {
loaders: [
{ test: /\.jsx?$/, loader: 'babel', }
]
}
};

If you want to run tests with babel then use:

mocha --compilers js:babel-core/register --require babel-polyfill

Babel 7 - ReferenceError: regeneratorRuntime is not defined

Updated Answer:

If you are using Babel 7.4.0 or newer, then @babel/polyfill has been deprecated. Instead, you will want to use the following at the top of your main js file (likely index.js or similar):

import "core-js/stable";
import "regenerator-runtime/runtime";

Install these packages either with npm:

npm install --save core-js
npm install --save regenerator-runtime

or with yarn:

yarn add core-js
yarn add regenerator-runtime

Original Answer:

I just encountered this problem and came across the following solution:

In package.json I had @babel/polyfill as a dependency. However, in my index.js (My main js file) I had neglected to place the following line at the the top:

import '@babel/polyfill'

Once I imported it, everything worked fine.

I did not need to install babel-runtime as other answers are suggesting.

Uncaught ReferenceError: regeneratorRuntime is not defined in react 17, webpack 5 while making api calls through actions

For Babel 7, install these two dependencies:

npm install --save @babel/runtime 
npm install --save-dev @babel/plugin-transform-runtime

And, in .babelrc, add:

{
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/plugin-transform-runtime"]
]
}

regeneratorRuntime is not defined Gulp + Webpack + Babel

So, after 2 days of searching and testing I finally resolved this issue:
1. Clean webpack works perfectly with recommended config:

package.json

{
"name": "babel-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack --config webpack.config.js --color"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.10.2",
"@babel/plugin-transform-runtime": "^7.10.1",
"@babel/preset-env": "^7.10.2",
"babel-loader": "^8.1.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
}
}

webpack.config.js

const path = require('path');
module.exports = {
mode: 'development',
entry: {
index: './index.js'
},
output: {
path: path.resolve(__dirname, 'public'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: [
["@babel/plugin-transform-runtime", { "regenerator": true }
]
]
}
}
}
]
}
}

  1. I updated all webpack and babel related packages to the latest stable versions.

  2. There was a problem in my gulp file. So I changed usage of webpack through webpack-stream. Before:

   const webpack = require('webpack');
gulp.task('scripts', function (callback) {
webpack(require('./webpack.config.js'), function (err, stats) {
if (err) {
console.log(err.toString());
}
console.log(stats.toString());
callback();
});
});

After:

 const webpack = require('webpack-stream');
gulp.task('scripts', function() {
return gulp.src(settings.themeLocation + "js/scripts.js")
.pipe(webpack( require('./webpack.config.js') ))
.pipe(gulp.dest(settings.themeLocation + "js"));
});

after rollup build: regeneratorRuntime is not defined

It's been almost 2 years since I last used rollup. Anyway in a project I found (where we faced the same issues), I saw the following plugins order:

plugins: [
[
'@babel/plugin-transform-runtime',
],
["@babel/plugin-proposal-optional-chaining"],
["@babel/plugin-proposal-nullish-coalescing-operator"],
["babel-plugin-transform-for-of-without-iterator"],
]

The @babel/plugin-transform-runtime does the magic.

npm i @babel/plugin-transform-runtime

To install



Related Topics



Leave a reply



Submit