【问题标题】:Webpack failing to pack https from Nodejs in Electron applicationWebpack 无法在 Electron 应用程序中从 Nodejs 打包 https
【发布时间】:2021-02-15 00:13:57
【问题描述】:

我的 webpack 配置似乎已成功构建,但是尽管在配置中定义了导入,但我得到了对 https 包的未定义引用。

我不确定我的 webpack 是否配置不正确,或者我在定义 axios 实例时是否以某种方式错误地调用了 https。任何指针将不胜感激。

webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");
const BUILD_DIR = path.resolve(__dirname, "./build/app");
const SRC_DIR = path.resolve(__dirname, "./src/app");

module.exports = {
  entry: {
    main: SRC_DIR + "/index.tsx",
  },
  output: {
    filename: "bundle.js",
    path: BUILD_DIR,
  },
  resolve: {extensions: [".ts", ".tsx", ".js", ".jsx"]},
  module: {
    rules: [
      {test: /\.tsx?$/, include: SRC_DIR, use: "awesome-typescript-loader"},
      {test: /\.(j|t)sx?$/, enforce: "pre", use: "source-map-loader"},
      {test: /\.css$/, use: ["style-loader", "css-loader"]},
      {test: /\.(jpg|png|gif|jpeg|woff|woff2|eot|ttf|svg)$/, use: "url-loader?limit=100000"},
      {test: /\.html$/, use: "html-loader"},
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/app/html/index.html",
      filename: "./index.html",
    }),
    new webpack.ExternalsPlugin("commonjs", ["electron", "https"]),
  ],
  target: "electron-renderer",
};

Client Instance

import Axios from "axios";
import https from "https";

export const clientInstance = Axios.create({
  baseURL: "https://127.0.0.1:2999/liveclientdata/allgamedata",
  httpsAgent: new https.Agent({ // <---- this is where it fails. the reference to https.
    rejectUnauthorized: false,
  }),
});

error

Uncaught ReferenceError: require is not defined
    at Object.https (bundle.js:7743)
    at __webpack_require__ (bundle.js:7766)
    at eval (webpack://express-electron-core/./src/app/components/plugins/axios.ts?:9)
    at Object../src/app/components/plugins/axios.ts (bundle.js:316)
    at __webpack_require__ (bundle.js:7766)
    at eval (webpack://express-electron-core/./src/app/components/hooks/useAssociatedAccounts.ts?:5)
    at Object../src/app/components/hooks/useAssociatedAccounts.ts (bundle.js:173)
    at __webpack_require__ (bundle.js:7766)
    at eval (webpack://express-electron-core/./src/app/components/hooks/index.ts?:4)
    at Object../src/app/components/hooks/index.ts (bundle.js:140)

【问题讨论】:

  • https 库就在节点上,它从来没有被 webpack 打包。你是在浏览器中尝试使用 axios 吗?
  • @trognanders 不,在电子窗口中。

标签: javascript node.js reactjs webpack electron


【解决方案1】:

main.ts

const mainWindow = new BrowserWindow({
  webPreferences:{
    contextIsolation: true //this was the issue
  }
});

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 2020-08-16
    • 2015-11-23
    • 2017-08-31
    • 2018-08-30
    • 2023-02-26
    • 2016-05-05
    • 2016-08-26
    相关资源
    最近更新 更多