Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegeneratorRuntime is not defined

I am trying to run Karma-babel-preprocessor and a straight forward ES6 generator:

//require('babel/polyfill');

  describe("how Generators work", function() {
    it("will allow generator functions", function() {
      /*function * numbers() {
        yield 1;
        yield 2;
        yield 3;
      };*/


      let numbers = {
        [Symbol.iterator]:function*(){
            yield 1;
            yield 2;
            yield 3;
          }
      }

      let sum = 0;

      for(n of numbers){
        sum += n;
      }

      expect(sum).toBe(6);
    });
  });

From this I generated my test files (ES6 => ES5) with babel:

babel src --watch --out-dir tests

Then I run karma start I get error:

ReferenceError: regeneratorRuntime is not defined".

Relevant bits in karma.conf.js:

  // list of files / patterns to load in the browser
    files: [
      'test-main.js',
      {pattern: 'tests/*.js', included: true}
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
            'src/*.js': ['babel']
    },
        'babelPreprocessor': {
      options: {
        sourceMap: 'inline'
      },
      filename: function(file) {
        return file.originalPath.replace(/\.js$/, '.es5.js');
      },
      sourceFileName: function(file) {
        return file.originalPath;
      }
    },


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],

Full project on github

I am able to use many ES6 features including arrows. Just no go on Generators.

like image 744
P.Brian.Mackey Avatar asked Mar 11 '15 00:03

P.Brian.Mackey


People also ask

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.)

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 babel runtime?

babel-runtime is a package that contains a polyfill and many other things that Babel can reference. You'd install it in your app with npm install babel-runtime. transform-runtime is a Babel plugin to process your source code and inject import foo from "babel-runtime" statements so that babel-runtime is actually used.


2 Answers

Node js Env - updated December 2015

This question has already been answered, please see accepted answer UNLESS running within NodeJS environment.

If like myself, you had the same error message: 'ReferenceError: regeneratorRuntime is not defined' but were running Babel within a NodeJS environment, then simply doing the following will likely solve your problem:

npm install babel-polyfill --save

Then insert the following require statement towards the top of the affected module to obtain required (generator) behaviour:

require("babel-polyfill");

This should be all you need, just importing the module adds required polyfill behaviour at runtime.

like image 108
arcseldon Avatar answered Sep 19 '22 04:09

arcseldon


Similar to the post by arcseldon, I was running Babel within a NodeJS environment and getting the same error message 'ReferenceError: regeneratorRuntime is not defined'. While installing babel-polyfill does work, I went with @babel/plugin-transform-runtime instead.

@babel/plugin-transform-runtime

It needs to be installed in two ways ... first as a dev dependency:

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

and second as a production dependency:

npm install --save @babel/runtime

And then there needs to be one simple addition to your .babelrc file:

{
  "plugins": ["@babel/plugin-transform-runtime"]
}

These additions give ES6 authoring functionality without the ReferenceError.

like image 41
Kevin Avatar answered Sep 19 '22 04:09

Kevin