较低版本的electron

enableRemoteModule:trueelectronremote
const remote = require('electron');

较高版本的electron(以我的19.0.8为例)

@electron/remote
npm install --save @electron/remote
enableRemoteModule:true
require('@electron/remote/main').initialize()
require("@electron/remote/main").enable(mainWin.webContents)

main.js

const { app, BrowserWindow } = require('electron')

function createWindow() {
    let mainWin = new BrowserWindow({
        show: false,
        width: 800,
        height: 600,
        // frame:false, //用于自定义menu,设置为false可以将默认的菜单栏隐藏
        // transparent:true,
        autoHideMenuBar: true,
        icon: "logo.ico", //设置应用的显示图标(转ico格式https://cloudconvert.com/)
        title: "测试标题", // 自定义当前应用的显示标题
        webPreferences: {
            nodeIntegration: true, //渲染进程是否集成 Nodejs
            contextIsolation: false,
            enableRemoteModule: true //是否允许渲染进程使用远程模块
        }
    })
    require('@electron/remote/main').initialize()
    require("@electron/remote/main").enable(mainWin.webContents)
    mainWin.loadFile('index.html')
    mainWin.on('ready-to-show', () => {
        mainWin.show()
    })
    mainWin.on('close', () => {
        console.log('mainWin is closed')
        mainWin = null
    })
}

app.on('ready', createWindow)
app.on('window-all-closed', () => {
    console.log('all window is closed')
    app.quit()
})
remote
const remote = require("@electron/remote")
remote
const remote = require("@electron/remote")

window.addEventListener('DOMContentLoaded', () => {
    //点击按钮打开一个新窗口
    const oBtn = document.getElementById('btn')
    oBtn.addEventListener('click', () => {
        //如何创建新窗口
        let indexMin = new remote.BrowserWindow({
            width: 200,
            height: 200
        })
        indexMin.loadFile('list.html')
        indexMin.on('close', () => {
            indexMin = null
        })
    })
})