You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron 15如何导入Menu?render.js引入时Remote未定义报错怎么解决

问题原因

你遇到的报错是因为@electron/remote没有在主进程完成完整的初始化配置,仅开启nodeIntegration和关闭contextIsolation不足以让remote模块正常工作。

正确实现步骤

1. 确认安装依赖

首先确保你的项目已经安装了@electron/remote包,未安装的话执行对应安装命令:
npm install @electron/remote --save

2. 修改主进程index.js代码

你现有代码缺失@electron/remote的主进程初始化逻辑,调整后的完整代码如下:

const { app, BrowserWindow } = require('electron');
const path = require('path');
// 引入@electron/remote主进程模块
const remoteMain = require('@electron/remote/main');
// 初始化remote能力
remoteMain.initialize();

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
    }
  });
  // 给当前窗口的webContents启用remote访问权限
  remoteMain.enable(mainWindow.webContents);

  // 此处保留你原有加载页面的逻辑,比如loadFile、loadURL等代码
};

// 原有app监听ready、window-all-closed等逻辑保持不变即可

3. 渲染进程代码无需修改

你现有render.js的导入逻辑是正确的,主进程配置完成后即可正常打印Menu对象,不会再抛出undefined或读取members相关的报错。

补充说明

Electron 14及以上版本已经移除了内置的remote模块,所有需要用到remote能力的场景都需要单独安装@electron/remote包并完成上述主进程配置才能正常使用。

内容的提问来源于stack exchange,提问作者Auax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 15:06:07