I’m learning electronjs and I’m trying to make a window that creates another window through a button, but when I was testing I noticed that one.js is not importing correctly the remote module. Heres the main.js file:
console.log('main process working');
console.log('main.js');
const { app, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');
const { initialize, enable } = require('@electron/remote/main');
initialize();
function createWindows() {
let win1 = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
});
let win2 = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
});
win1.loadURL(url.format({
pathname: path.join(__dirname, 'one.html'),
protocol: 'file',
slashes: true
}));
win2.loadURL(url.format({
pathname: path.join(__dirname, 'two.html'),
protocol: 'file',
slashes: true
}));
win1.webContents.openDevTools();
win2.webContents.openDevTools();
win1.on('closed', () => {
win1 = null;
});
win2.on('closed', () => {
win2 = null;
});
app.on('browser-window-created', (_, window) => {
enable(window.webContents);
});
}
app.on('ready', createWindows);
And here is one.js file:
console.log('From renderer 1');
const { ipcRenderer, remote } = require('electron');
const path = require('path');
const url = require('url');
const { enable } = require('@electron/remote/main');
enable(ipcRenderer);
ipcRenderer.on('electron-module', (event, electron) => {
console.log('Electron module received');
const newWindowBtn = document.getElementById('Btn');
newWindowBtn.addEventListener('click', function (event) {
console.log('Botão clicado!');
let win3 = new remote.BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
});
win3.loadURL(url.format({
pathname: path.join(__dirname, 'three.html'),
protocol: 'file',
slashes: true
}));
win3.webContents.openDevTools();
});
});
ipcRenderer.send('get-electron-module');
Main creates two windows, and window 1 has a button that when clicked should create a new window.
While trying to make this work I noticed that the button doest work.
New contributor
user24675945 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.