Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel 6 regeneratorRuntime is not defined

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?

like image 937
BrunoLM Avatar asked Nov 04 '15 16:11

BrunoLM


People also ask

Where do I import regenerator-runtime?

You can either import "regenerator-runtime/runtime" in every entry file (not recommended if you don't need the polyfill!), or you can tell babel what your runtime is. You should be able to get it to work with the @babel/preset-env preset, configured like so in your .

What is regenerator-runtime min JS?

regenerator-runtime is the runtime support for compiled/transpiled async functions. (It may well have other uses, but this is the predominant one.)

How do you use Babel runtime?

How to use it. Assuming you have a running Babel environment: Install babel-plugin-transform-runtime (as a devDependency ), which transforms your code to remove the helpers and uses the ones in babel-runtime . You need to add it to the plugins array of your Babel configuration.


2 Answers

Note If you're using babel 7, the package has been renamed to @babel/plugin-transform-runtime.

Besides polyfill, I use babel-plugin-transform-runtime. The plugin is described as:

Externalize references to helpers and builtins, automatically polyfilling your code without polluting globals. What does this actually mean though? Basically, you can use built-ins such as Promise, Set, Symbol etc as well use all the Babel features that require a polyfill seamlessly, without global pollution, making it extremely suitable for libraries.

It also includes support for async/await along with other built-ins of ES 6.

$ npm install --save-dev babel-plugin-transform-runtime 

In .babelrc, add the runtime plugin

{   "plugins": [     ["transform-runtime", {       "regenerator": true     }]   ] } 
like image 20
johnny Avatar answered Sep 24 '22 12:09

johnny


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 
like image 177
BrunoLM Avatar answered Sep 24 '22 12:09

BrunoLM