"regeneratorRuntime is not defined" after upgrading to 1.4.2.1

I was running on 1.4.2.1-rc.1 fine. However after upgrading in 1.4.2.1 proper and adding babel-runtime npm package I now get ReferenceError: regeneratorRuntime is not defined in the browser. Everything I can find online says the answer to this is properly including babel-polyfill but I have always been doing that and it’s the first import in my both of my main.js.

If I check in the browser console then global._babelPolyfill is true so I’ve definitely got the polyfill included correctly. But looking through the regenerator code (it’s in my modules.js in the browser) I should have global.regeneratorRuntime but that’s not there.

Has anyone else struck this problem?

So far I’ve tried:

  • meteor reset
  • Removing babel-runtime
  • Upgrading all my npm packages

No dice. I tried to make a small reproduction but can’t get it to fail, so it’s obviously something I have in my projects (it happens in all 3 I’ve upgraded).

@benjamn I know this is something specific to my project setup but is obviously related to the change in Meteor’s handling of babel runtime helpers. Any hints as to what to try next?

You’re probably using some older code (perhaps in a precompiled npm package) that assumes regeneratorRuntime is globally defined, instead of importing it explicitly. You can either track that code down and update it, or just define global.regeneratorRuntime in your own code:

global.regeneratorRuntime = require("babel-runtime/regenerator");
1 Like

Thanks @benjamn. Trying the first 2 lines of my client/main.js being:

import 'babel-polyfill'
global.regeneratorRuntime = require('babel-runtime/regenerator')

I still get the error. And then in the browser console global.regeneratorRuntime is still undefined. Which seems crazy. But in regenerator-runtime/runtime-module.js there is this which might be doing that:

  // Remove the global property added by runtime.js.                                                                   // 25
  try {                                                                                                                // 26
    delete g.regeneratorRuntime;                                                                                       // 27
  } catch(e) {                                                                                                         // 28
    g.regeneratorRuntime = undefined;                                                                                  // 29
  }                                                                                                                    // 30

I’ve also tried wiping my node_modules and reinstalling. No luck.

I think you’re right about that delete g.regeneratorRuntime code, alas.

This isn’t a great solution, but it might help you get your app working again in the short term:

Object.defineProperty(global, "regeneratorRuntime", {
  value: require("babel-runtime/regenerator"),
  enumerable: true,
  // Prevents reassignment:
  writable: false,
  // Prevents delete global.regeneratorRuntime:
  configurable: false
});

More investigation showed some of my lack of understanding. The line global.regeneratorRuntime = require('babel-runtime/regenerator') didn’t work because all the imports in main.jsx are hoisted above it, and it’s one of the imports that causes the issue. So setting global.regeneratorRuntime in the right place does work.

And I figured out the root cause. My own npm package wasn’t using babel-plugin-transform-runtime. I gather with how Meteor uses babel-runtime this is mandatory for all packages we import now?

thx , work perfectly for me