s-leg3ndz
s-leg3ndz

Reputation: 3528

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.

Upvotes: 9

Views: 15442

Answers (4)

Ismael Antonio
Ismael Antonio

Reputation: 141

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

Upvotes: 1

Mohan Teja Chitturi
Mohan Teja Chitturi

Reputation: 169

for me I used this in the transpiled code for successful run :

require("@babel/polyfill");

Upvotes: 0

codeBelt
codeBelt

Reputation: 1815

I had to add @babel/plugin-transform-runtime and set runtimeHelpers: true.

rollup.config.js

import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import pkg from './package.json';

const extensions = [
    '.js', '.jsx', '.ts', '.tsx',
];

const name = 'RollupTypeScriptBabel';

export default {
    input: './src/index.ts',

    // Specify here external modules which you don't want to include in your bundle (for instance: 'lodash', 'moment' etc.)
    // https://rollupjs.org/guide/en#external-e-external
    external: [...Object.keys(pkg.dependencies || {})],

    plugins: [
        // Allows node_modules resolution
        resolve({extensions}),

        // Allow bundling cjs modules. Rollup doesn't understand cjs
        commonjs(),

        // Compile TypeScript/JavaScript files
        babel({extensions, include: ['src/**/*'], runtimeHelpers: true}),
    ],

    output: [{
        file: pkg.main,
        format: 'cjs',
    }, {
        file: pkg.module,
        format: 'es',
    }],
};

.babelrc

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

https://github.com/codeBelt/generate-template-files

https://github.com/a-tarasyuk/rollup-typescript-babel

Upvotes: 0

Michał Perłakowski
Michał Perłakowski

Reputation: 92639

You should import the Babel Polyfill in your code:

import "@babel/polyfill";

Upvotes: 25

Related Questions