Within my electron application, I make use of fork from the child_process library to fork the process so I can have the functionality to stop/return the function instantly, but the problem I'm having is, it works perfectly fine within VSCode, but whenever I package the application into an exe using electron-builder, the script just prematurely exits instead of executing and there's no errors being logged as to why it's exiting. Does anyone have any ideas as to how can I properly do this? I read somewhere that spawning child processes in electron application in production can be problematic due to pathing issues.
index.js
ipcMain.handle('run-login', async (event, args) => {
const taskProcessesPath = path.join(__dirname, '../build/taskProcesses.js');
const child = fork(taskProcessesPath, [JSON.stringify({ ...args, taskType: 'login' })]);
taskProcesses[args.id] = child;
child.on('message', (message) => {
console.log('Message From Task:', message);
});
child.on('exit', (code) => {
statusUpdate(args.id, 'Task Stopped', 'error');
taskGroupUpdate(args.taskGroupId, 'error');
runningUpdate(args.id, false);
delete taskProcesses[args.id];
});
});
taskProcesses.js
const { login } = require('../Modules/login');
async function runTask(args) {
try {
switch (args.taskType) {
case 'login':
await login(args.taskGroupId, args.id, args.proxyGroup);
break;
default:
throw new Error(`Unsupported task type: ${args.taskType}`);
}
process.send({ status: 'completed' });
process.exit(0);
} catch (error) {
console.error(`Error executing ${args.taskType} task:`, error);
process.send({ status: 'error', error: error.message });
process.exit(1);
}
}
if (require.main === module) {
const args = JSON.parse(process.argv[2]);
runTask(args);
}
module.exports = { runTask };
It doesn't look like your process is spawning at all, you're not seeing an error from it because you aren't listening for it. I imagine you'll see something if you adjust your code like so.
You may be running into an issue with using linux path separators on windows, you're already using
path.join, you should use it fully. i.e.path.join(__dirname, '..','build','taskProcesses.js'), or you can lose the '..' in the end result withpath.resolve(__dirname,'..','build','taskProcesses.js').You may also be running into not having taskProcesses.js exist in your build at all, which would be an issue in your package.json setup.