When I built npm start
the app on Windows 10, it does not work normally which worked fine on macOS.
package.json
{
"name": "SimpleTimer",
"version": "1.0.0",
"productName": "SimpleTimer",
"copyright": "",
"description": "",
"main": "main.js",
"scripts": {
"start": "node_modules/.bin/electron .",
"test": "echo \"Error: no test specified\" && exit 1",
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"electron": "^10.1.2"
}
}
I got the following error message.
TypeError: ipcMain.handle is not a function
where this applies:
main.js
ipcMain.handle("ipc-timer-start", () => {
if ( isWorking === true ) {
return true
}
else {
StartTimer();
isWorking = true;
}
return true;
});
This function is the recipient of the ipc communication from ipcRenderer.invoke()
written in preload.js
. That's the invoke()
method in TimerStart
api in the following source.
preload.js
const { contextBridge, ipcRenderer} = require("electron");
contextBridge.exposeInMainWorld(
"api", {
TimerStart: () =>
ipcRenderer.invoke("ipc-timer-start") /*** HERE ***/
.then(result => result)
.catch(err => console.log(err)),
TimerStop: () => ipcRenderer.send("ipc-timer-stop"),
TimerReset: () => ipcRenderer.send("ipc-timer-reset"),
DisplayTimer: (channel, listener) => {
ipcRenderer.on("ipc-display-timer", (event, arg) => listener(arg));
}
}
);
Of course, preload.js
is specified when it created the BrowserWindow
.
main.js
mainWindow = new BrowserWindow({
title: config.name,
width: 1024,
height: 640,
minWidth: 1024,
minHeight: 640,
webPreferences: {
worldSafeExecuteJavaScript: true,
nodeIntegration: false,
contextIsolation: true,
preload: __dirname + '/preload.js' /*** HERE ***/
}
});
After skipping this error message dialog, I checked with the DevTools and it looks like preload.js
is not loaded from the statement of Unable to load preload script:
.
Given these statements, it seems that preload.js
is not properly included in the build, what should I do?
Once again, it worked fine on macOS, but it doesn't work properly on Windows 10 due to these issues.