0

最近,我尝试用 Snowpack 开始我的个人电子项目。我的项目基于electron-snowpack,起初一切都很好。但是当我尝试electron在渲染器进程中导入包或任何其他节点包时。发生错误。

import {remote} from 'electron' 
console.log(remote) 


我试过的导入电子错误如下:

  1. 设置 webPreferences:
nodeIntegration:true,
contextIsolation:false,
enableRemoteModule:true,

  1. 使用preload
// preload.js
const {
    contextBridge,
    ipcRenderer
} = require("electron");


contextBridge.exposeInMainWorld("api",{
    send:(channel, data)=>{
        // whitelist channels
        let validChannels = ["toMain"];
        if (validChannels.includes(channel)) {
            ipcRenderer.send(channel, data);
        }
    },
    receive: (channel, func) => {
        let validChannels = ["fromMain"];
        if (validChannels.includes(channel)) {
            // Deliberately strip event as it includes `sender`
            ipcRenderer.on(channel, (event, ...args) => func(...args));
        }
    }
})

// main.js
 const win = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      nodeIntegration: false, // is default value after Electron v5
      contextIsolation: true, // protect against prototype pollution
      enableRemoteModule: false, // turn off remote
      preload:"./preload.js"
    },
  })

// renderer.js
console.log(window.api)  // undefined

  1. window.require在主进程中定义。然后const electron = require("electron")。发生错误require is not a function

我尝试了所有我能找到的解决方案。但是错误仍然存​​在。有没有人可以给我任何帮助?谢谢!

4

0 回答 0