Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error regeneratorRuntime is not defined with Babel 7

I would like update my Babel config to version 7 in Electron project.

I've add my all plugins i needed :

"devDependencies": {
    "@babel/cli": "^7.0.0-beta.40",
    "@babel/core": "^7.0.0-beta.40",
    "@babel/node": "^7.0.0-beta.40",
    "@babel/plugin-proposal-class-properties": "^7.0.0-beta.40",
    "@babel/plugin-proposal-decorators": "^7.0.0-beta.40",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.40",
    "@babel/plugin-proposal-optional-chaining": "^7.0.0-beta.40",
    "@babel/plugin-transform-async-to-generator": "^7.0.0-beta.40",
    "@babel/polyfill": "^7.0.0-beta.40",
    "@babel/preset-env": "^7.0.0-beta.40",
    "@babel/preset-react": "^7.0.0-beta.40",
    "babel-eslint": "^7.1.1",

The compilation is good, but when Electron run my main.js (compiled), i've this error :

A JavaScript error occurred in the main process Uncaught Exception: ReferenceError: regeneratorRuntime is not defined

I've try to install regeneratorRuntime module, but no result.

like image 656
s-leg3ndz Avatar asked Mar 13 '18 10:03

s-leg3ndz


People also ask

What is babel polyfill?

Babel includes a polyfill that includes a custom regenerator runtime and core-js. This will emulate a full ES2015+ environment (no < Stage 4 proposals) and is intended to be used in an application rather than a library/tool. (this polyfill is automatically loaded when using babel-node ).

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.

How to install @Babel/plugin-transform-runtime without babelrc?

How we can do it without .babelrc (just using webpack config file)?. This error is caused when async/await functions are used without the proper Babel plugins. To install @babel/plugin-transform-runtime by typing in the command line,

Why does @Babel/preset-env choose to use the plugin-transform-regenerator instead of regeneratorruntime?

You have used at least one of these features and it's not supported in at least one of your target s ( ['last 2 versions', 'ie >= 9'] ): As a result, @babel/preset-env decides to use @babel/plugin-transform-regenerator, which relies on regeneratorRuntime being available globally.

How do I fix the async/await error in Babel?

This error is caused when async/await functions are used without the proper Babel plugins. To install @babel/plugin-transform-runtime by typing in the command line, npm install --save-dev @babel/plugin-transform-runtime Then Add the plugin to pakage.json file in plugins array.

Does @Babel/runtime conflict with @B Babel/polyfill?

If you use @babel/runtime only for the Babel helpers, it doesn't "conflict" with @babel/polyfill. Sorry, something went wrong. thx @nicolo-ribaudo, I thought both were mutually exclusive.


2 Answers

hey I ran into the same problem and I am using Babel 7, for me I installed these two dependencies:

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

And, in .babelrc, add:

 {
    "presets": ["@babel/preset-env"],
    "plugins": [
        ["@babel/transform-runtime"]
    ]
   }

and this solved my problem

like image 56
Ismael Antonio Avatar answered Oct 14 '22 22:10

Ismael Antonio


You should import the Babel Polyfill in your code:

import "@babel/polyfill";
like image 23
Michał Perłakowski Avatar answered Oct 14 '22 21:10

Michał Perłakowski