I have an electron app where I am trying to push some CPU intensive tasks to a worker thread. This works well when I start the app using npm start, but after packing the app (with electron-packager with --asar) the worker thread appears to stop working. I am loading the worker thread like so:
const workerThread = new SharedWorker(path.join(__dirname, "JS/JS_MainWindow/worker.js"));
I am very confused about what could be causing this, but could it maybe be that the appl is being packaged into an ASAR folder? I am very confident I have the path correct for loading the worker file as I am loading several other images with the sameish path.
My project was also trying to do background/parallel task processing from within an Electron app and we ran into the same issue: the background tasks run fine when run using npm run electron-dev but when a Windows installer is built, the application fails with an error saying it cannot find the module referenced in the worker JS script.
After researching the issue I concluded that Electron runs on a patched version of node that is aware of ASAR archives (Electron's archive format) that makes require() statements with paths work magically both in a packaged/archived environment within ASAR and in a dev environment within your source code.
Because the worker thread runs in a vanilla Node process, and not the patched electron node process, the worker thread cannot resolve the require() statement with a path inside the ASAR archive.
I tried to get our existing code working using the asar-node npm package, but wasn't successful.
Our project is currently trying to use a library called electron-workers to accomplish the same background work task, and we're running into different issues (no performance increase between multiple workers and just running on the main thread).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With