regeneratorRuntime is not defined

Troubleshooting the regeneratorRuntime runtime error after adding redux-saga-promise, covering root cause and fixes in Webpack and Jest.

Feb 2, 2020 · 1 min read · 183 Words · -Views -Comments · Programming
TypeScript 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

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.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover