Shared worker working before packing app, but not working after (electron)
Asked Answered
S

3

4

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.

Sway answered 9/12, 2019 at 17:43 Comment(7)
can you console.log(path.join(__dirname, "JS/JS_MainWindow/worker.js")) and inspect the path after packing? (enable developer tools to follow)Potty
Yes the path is correct after packaging... C:\FOD\FOD-JS\fodGUI-win32-x64\resources\app.asar\src\JS\JS_MainWindow\worker.jsSway
but..your file is already packed and no longer present there. maybe you can ignore it following this post github.com/electron-userland/electron-builder/issues/383 (very detailed.. follow the links)Potty
I may not be understanding this correctly, but I believe the .asar is a read-only archive and that the files/folders can still be accessed using paths like the one above? I am accessing some images using the same type of paths after packaging. edit: after a little bit more research I believe I am wrong about this... This linked helped a lot : github.com/electron-userland/electron-builder/issues/751Sway
There is a different between static paths (used before packing) and dynamic paths (used after packing) ... may I suggest that you share your insights here when you done (post your own answer) to help others as well?Potty
So something weird... fs.existsSync(path.join(__dirname, "JS/JS_MainWindow/worker.js")) returns true even after packaging. So the file is definitely there and its definitely at least seeable by the app post packaging.Sway
Okay... So the path is actually working and the worker is getting loaded in, the issue is that I am using require() to load in a couple of modules in my worker and for some reason that only works before packaging. after commenting out my require calls, the worker can send and receive messages from the main threadSway
S
0

So this error was caused by a certain module I was requiring within my worker thread. For some reason, this certain module doesn't work post packaging.The path was indeed correct and workers apparently do work post packaging using "same" path as before packaging (ex: "C:\FOD\FOD-JS\fodGUI-win32-x64\resources\app.asar\src\JS\JS_MainWindow\worker.js").

Sway answered 9/12, 2019 at 20:29 Comment(0)
E
5

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).

Eastbound answered 30/3, 2021 at 7:0 Comment(1)
Unbelievable. I can't see any logical reason to not make a worker use electron's patched fs. Electron has the absolute worst support for both web workers and worker threads.Vigue
S
0

So this error was caused by a certain module I was requiring within my worker thread. For some reason, this certain module doesn't work post packaging.The path was indeed correct and workers apparently do work post packaging using "same" path as before packaging (ex: "C:\FOD\FOD-JS\fodGUI-win32-x64\resources\app.asar\src\JS\JS_MainWindow\worker.js").

Sway answered 9/12, 2019 at 20:29 Comment(0)
M
0

I have a similar issue with my Electron app. After packaging the app with asar true the workerpool script can no longer find the node modules. A quick solution is to copy the node_modules folder into the packaged application directory. This way, the workerpool thread can have access to the node modules. This isn't a great solution, as we will be packaging the node modules twice in the folder and increases the size of application. but worked for me. MAy be adding nodemodules separately and not packaging in asar might work better. I will try that and post if i get any updates on this issue.

Mckay answered 15/10, 2023 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.