Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

regeneratorRuntime is not defined #26

Closed
adilsoncarvalho opened this issue Oct 31, 2018 · 2 comments
Closed

regeneratorRuntime is not defined #26

adilsoncarvalho opened this issue Oct 31, 2018 · 2 comments

Comments

@adilsoncarvalho
Copy link

Hi there.

Do you guys have an option on how to deal with the regeneratorRuntime is not defined issue?

I updates your index.ts and added the following method into the C class:

async metodo() {
  return "ok";
}

After building it and trying to invoke the transpiled code using node ./lib/index I got the following error:

$ node
> require("./lib/index")
ReferenceError: regeneratorRuntime is not defined
    at /Users/adilson/dev/TypeScript-Babel-Starter/lib/index.js:46:7
    at /Users/adilson/dev/TypeScript-Babel-Starter/lib/index.js:64:6
> 

That's because of this part of the generated code that requires regeneratorRuntime to work:

_createClass(C, [{
  key: "metodo",
  value: function () {
    var _metodo = _asyncToGenerator(
    /*#__PURE__*/
    regeneratorRuntime.mark(function _callee() {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              return _context.abrupt("return", "ok");

            case 1:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

    return function metodo() {
      return _metodo.apply(this, arguments);
    };
  }()
}]);

All the post I saw so far mention those two options:

  • @babel/plugin-transform-regenerator which I could not manage to get it working

  • to add @babel/polyfill that actually works but it is a terrible option when coding a new lib as there is no central import point, making the require to happen on each file

Do you have any tips on that?

Thanks!

@mng-au
Copy link

mng-au commented Nov 11, 2018

I am able to get regeneratorRuntime is not defined issue resolved today. I will note down what I found here.

Instead of @babel/plugin-transform-regenerator, add @babel/plugin-transform-runtime and @babel/runtime.

yarn add -D @babel/plugin-transform-runtime
yarn add @babel/runtime

Then add @babel/plugin-transform-runtime to .babelrc, and should look like the following:

{
    "presets": [
        "@babel/env",
        "@babel/typescript"
    ],
    "plugins": [
        "@babel/plugin-transform-runtime",
        "@babel/proposal-class-properties",
        "@babel/proposal-object-rest-spread"
    ]
}

Please let me know does it fix your issue with regeneratorRuntime is not defined. I will upload a small working webpack solution to github if necessary.

All the best.

References:

  1. https://babeljs.io/docs/en/next/babel-plugin-transform-runtime

@matthias-ccri
Copy link

Does babel always transform async/await statements to regeneratorRuntime calls? If so — should this be added to the recommended setup in the README, given that async/await is a core part of typescript?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants