【问题标题】:Error regeneratorRuntime is not defined with Babel 7错误 regeneratorRuntime 未使用 Babel 7 定义
【发布时间】:2021-10-15 04:39:01
【问题描述】:

我想在 Electron 项目中将我的 Babel 配置更新到版本 7。

我已经添加了我需要的所有插件:

"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",

编译很好,但是当 Electron 运行我的main.js(已编译)时,我出现了这个错误:

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

我尝试安装regeneratorRuntime 模块,但没有结果。

【问题讨论】:

    标签: node.js electron babeljs


    【解决方案1】:

    您应该在代码中导入 Babel Polyfill:

    import "@babel/polyfill";
    

    【讨论】:

    • 具体在哪里?
    • @hecontreraso 在主文件的顶部。
    • 但你为什么要导入它?
    • 编译后还是编译前?
    【解决方案2】:

    嘿,我遇到了同样的问题,我正在使用 Babel 7,我安装了这两个依赖项:

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

    并且,在 .babelrc 中,添加:

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

    这解决了我的问题

    【讨论】:

      【解决方案3】:

      我必须添加@babel/plugin-transform-runtime 并设置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

      【讨论】:

        【解决方案4】:

        对我来说,我在转译代码中使用它来成功运行:

        require("@babel/polyfill");

        【讨论】:

          猜你喜欢
          • 2019-05-02
          • 2021-09-26
          • 2016-02-05
          • 2016-10-29
          • 2020-09-20
          相关资源
          最近更新 更多