首页 > 解决方案 > 不能在带有 TypeScript Create React App 和预加载脚本的 Electron 中使用`require`

问题描述

我正在尝试ipcRenderer在 (Create)React(app) 代码中导入 Electron,但遇到了障碍:

  1. 未执行预加载脚本:

在此处输入图像描述

new BrowserWindow({
      titleBarStyle: "customButtonsOnHover",
      resizable: false,
      width: config.width,
      height: config.height,
      webPreferences: {
        contextIsolation: false,
        nodeIntegration: true,
        preload: path.join(__dirname, "..", "preload.js"),
      },
    })
  1. 我尝试将脚本添加到 index.html
  <script>
      console.log(require);
  </script>

这导致:

要求未定义

似乎是由于 webpack 路径无法正常工作?哪种解决方案适用于将电子 ipcRenderer 导入 React 代码?

标签: electron

解决方案


还需要为视图启用节点:

 new BrowserView({
    webPreferences: {
      nodeIntegration: true,
    },
  })

推荐阅读