regeneratorRuntime is not defined

regeneratorRuntime is not defined

2月 2, 2020 · 1 分钟阅读时长 · 183 字 · -阅读 -评论

After introducing @adobe/redux-saga-promise, the frontend refused to boot with a regeneratorRuntime is not defined error. Here’s the investigation and fix.

What Is regenerator-runtime?

Source transformer enabling ECMAScript 6 generator functions in JavaScript-of-today.

In short, it polyfills generator and async function support for compiled JavaScript.

Official docs: https://github.com/facebook/regenerator/tree/master/packages/regenerator-runtime

Checking package dependencies

  • @adobe/redux-saga-promise lists "regenerator-runtime": "^0.13.3" in devDependencies, so the error is tied to this addition.
  • The package uses the runtime during tests but expects it to exist at runtime as well.

TypeScript configuration

The project compiles to ES6. Generators and async/await are part of ES6, and the codebase already runs redux-saga without issues. Why does adding redux-saga-promise crash?

Looking at its source, the package directly references regeneratorRuntime. The compiled output targets ES5, so regeneratorRuntime is effectively a production dependency. Even with an ES6 target, you still need to include the runtime when using this package.

Fixes

Webpack

new webpack.ProvidePlugin({
  regeneratorRuntime: 'regenerator-runtime/runtime'
});

Jest

In your test setup file (e.g., enzyme-setup.ts):

import 'regenerator-runtime/runtime';

Bundle impact

Negligible—the runtime is under 1 KB when compressed.

Final Thoughts

Identify the root cause first; the fix often follows naturally.

Alan H
Authors
开发者,数码产品爱好者,喜欢折腾,喜欢分享,喜欢开源