You need to enable JavaScript to run this app. 已开启enableRemoteModule但Electron.remote仍为undefined问题求助
优惠活动
大模型
产品
解决方案
定价
更多

已开启enableRemoteModule但Electron.remote仍为undefined问题求助

问题原因

你遇到的是Electron版本迭代带来的API变更问题:Electron 14及以上版本已经移除了内置的remote模块,即使在webPreferences中设置enableRemoteModule: true也不会生效,需要使用官方拆分出来的独立包@electron/remote替代。

解决步骤

  • 第一步:在项目根目录执行命令安装依赖:
    npm install @electron/remote --save

  • 第二步:修改主进程main.js代码:
    首先引入@electron/remote的主进程模块并初始化,同时为你的窗口开启remote权限,删除无效的enableRemoteModule配置,修改后完整代码如下:

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

function createWindow () {
  const mainWindow = new BrowserWindow({
    width: 530,
    height: 330,
    resizable: false,
    frame : false,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      preload: path.join(__dirname, 'preload.js')
    }
  })
  // 为当前窗口启用remote权限
  remoteMain.enable(mainWindow.webContents)
  mainWindow.loadFile('index.html')
  mainWindow.webContents.openDevTools();
}

app.whenReady().then(() => {
  createWindow()
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})
  • 第三步:修改渲染进程的JS代码,调整remote引入方式,同时修正你代码里的变量名拼写错误(你定义的变量是remote_v,但有一行写成了remote),修改后代码如下:
const $ = require('jquery');
// 从独立包引入remote
const remote_v = require("@electron/remote");
// 修正变量名
var win = remote_v.getCurrentWindow();

$('#minimize').click(function(){
  remote_v.BrowserWindow.getFocusedWindow().minimize();
});

$('#close').click(function(){
  remote_v.getCurrentWindow().close();
});

修改完成后重新启动项目即可正常使用remote相关能力。

内容的提问来源于stack exchange,提问作者Nguyễn Võ Xuân Duy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 03:03:03