2

I got this error when I try to render Button from ant design in my Electron project, and it renders nothing instead. When I change from button to just Hello World text, it works.

I'm not sure whether the error comes from webpack or the ant design itself.

Note: I manually create this project by myself, but the error can be reproduced on https://github.com/Devtography/electron-react-typescript-webpack-boilerplate but no error on https://github.com/Robinfr/electron-react-typescript

Error message

Uncaught ReferenceError: global is not defined
    at Object../node_modules/fbjs/lib/setImmediate.js (renderer.js:58171)
    at __webpack_require__ (renderer.js:20)
    at Object../node_modules/draft-js/lib/editOnBeforeInput.js (renderer.js:49367)
    at __webpack_require__ (renderer.js:20)
    at Object../node_modules/draft-js/lib/DraftEditorEditHandler.js (renderer.js:45296)
    at __webpack_require__ (renderer.js:20)
    at Object../node_modules/draft-js/lib/DraftEditor.react.js (renderer.js:44011)
    at __webpack_require__ (renderer.js:20)
    at Object../node_modules/draft-js/lib/Draft.js (renderer.js:43920)
    at __webpack_require__ (renderer.js:20)

webpack.config.js

const path = require("path");

const config = {
  target: "electron-main",
  devtool: "source-map",
  entry: "./src/main.ts",
  output: {
    filename: "main.js",
    path: path.resolve(__dirname, "dist")
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js"]
  },
  node: {
    __dirname: false,
    __filename: false
  }
};

module.exports = (env, argv) => {
  return config;
};

webpack.react.config.js

const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");

const htmlPlugin = new HtmlWebPackPlugin({
  template: "./src/index.html",
  filename: "./index.html"
});

const config = {
  target: "electron-renderer",
  devtool: "source-map",
  entry: "./src/app/renderer.tsx",
  output: {
    filename: "renderer.js",
    path: path.resolve(__dirname, "dist")
  },
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        loaders: ['style-loader', 'css-loader']
      }
    ]
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js"]
  },
  plugins: [htmlPlugin]
};

module.exports = (env, argv) => {
  return config;
};
Sherazin
  • 21
  • 1
  • 3

3 Answers3

4

Just to add some context to the provided answer, add this in you index.html file (inside the markups):

<script>
  const global = globalThis;
</script>
manu
  • 1,059
  • 1
  • 16
  • 33
2

This is related to https://github.com/facebook/fbjs/issues/290.

Fix it by adding a global object on the window:

(window as any).global = window;
oreofeolurin
  • 636
  • 3
  • 16
0

Had the same issue, what I did before any script execution:

global = globalThis;
l-portet
  • 636
  • 7
  • 14