You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Having the same issue with Electron 4.x.x and Node 12.4. Same behavior and setup. Have you found a fix for this?
I've tried:
export NODE_OPTIONS=--experimental-worker in both the runtime script && in my ~/.bash_profile
Running electron as a node process via the RUN_ELECTRON_AS_NODE=1 flag
I've switched to using a global.window.Worker (web worker) for now, but would love to implement this lib in my project as soon as possible. If anyone can shed some light on this, or share a working example, that'd be great!
edit: @sofianguy maybe you can lead us in the right direction, or give us a yes/no as to whether this is working in v4 or 5
I've tried it before even posting this issue. It doesn't change anything. Experimental flag for worker module was only required in node v11 if I remember correctly.
wow, our watch-wiki-worker.js dependes on my local git.js and git.js depends on chokidar and dugite.
seems chokidar and dugite depends on tons of things, and things depend on things... So I have to use npx npm-remote-ls --flatten chokidar -d false -o false and so on to get the flattened dependency tree...
If you miss one of the dep of dep, you will get MODULE_NOT_FOUND error too.
list all 3rd party package that is required in worker_threads script, in the asarUnpack
use extraResources to copy worker_threads scripts and local files that are required in worker_threads script, to app.asar.unpacked
This solution works for me as well
I was not able to use worker_threads with the following configuration:
I've used Electron Workers described here. Tasks processing is x2 faster now. I had a lot of sex love with Electron before I made it works but it is doable. it was really hard to get the list of all the dependencies(50) I have to add to asarUnpack. It's weird that so powerfull feature not documented well and needs so a lot of tricks to do. I would be happy to get know that I can do something simpler to make it works in another way even after all this hell.
@User0111 Actually I move to electron-forge, and use threads-plugin, so webpack pack standalone js file for worker to run, and it works fine and no asar-unpack now.
Electron really need to add more support for worker_threads especially since it's a god given for GUI development. I still find it very weird why the worker is unable to use Electron's patched fs. The most of my time I've spent debugging my projects, is related to Electron + asar + worker threads. There's always a "gotcha" waiting around the corner when going into production. @linonetwo I see that you're going through the same exact hell as I am since I see you on every worker_threads related issue that I'm also having.
Reacted by Coukaratcha, Mike Jerred, Victória and KoningWouter
That's great. Unfortunately I cannot afford the time for any major refactoring right now. I'm just using my workers and dependencies unpacked for the moment. But love to move to a solid solution in the near future.
@linonetwo Thank you for your help. I have used the asar.unpacked trick and it worked like a charm. However, I am very disappointed that worker_threads are not working properly out of the box when the app is packaged.
I see that you found a better solution with electron-forge but still I think it should be great that electron improves worker_threads integration. I am not satisfied with @codebytere's reply as I think that the issue was misunderstood: the issue is not that worker_threads are not working in renderer process, but that it cannot be possible to load modules from worker when using worker_threads from the main process. I think unpacking modules, even if it is a clever idea and a good temporary workaround, is not a good solution as we lost the perks of packaging.
Is there any better solution if we want to keep nsis build target?
@linonetwo Thanks for the help. The mentioned solution did work but my worker is also using 'electron-store' which internally uses 'electron' Module. 'electron' needs to be mentioned in the dev-dependecies and I am not able to unpack the modules mentioned in the dev-dependencies using "asarUnpack" in package.json. Any workaround for this ?
I am getting below error
@JaspreetChhabra I think code in worker don't have access to electron modules, because electron module only accessible from electron main thread (not any worker thread).
A workaround may be passing signal to trigger things in main thread and return data back to the worker thread. You may use threads plugin to simplify this, it supports rxjs, so passing things here and there is not that terrible.
I think unpacking modules, even if it is a clever idea and a good temporary workaround, is not a good solution as we lost the perks of packaging.
I don't think electron will do anything new on this, because worker_thread is working with simple npm dependency.
It depends on the npm package, if it use fs and path to locate other file, you have to move them to resource/ folder in afterPack script. I'm always doing this.
But I'm not sure now, because @giladgd say it is possible to locate .node dependency without manually move them? withcatai/node-llama-cpp#199 (comment)
I'm currently only move .node files to resource/ folder in afterBuild.js, and other JS dependency are bundled by electron-forge and works correctly in worker_thread.
@linonetwo Thanks for reply. In my case, I have an SDK, the SDK calls a .dll based on ffi-napi, I configured SDK externals in webpack, and move the SDK to asar.unpacked folder, which runs well in main thread;
But now I want to call this SDK in worker_threads, it report an error MODULE_NOT_FOUND, to solve this problem, I need to bundle the SDK and worker's JS code together? and move the .node file in SDK's dependencies(ffi-napi ... ) to asar.unpacked folder ?
The main reason is that utilityProcess doesn't know where to find node_modules.
With option asar: true electron package all of them (except native modules see AutoUnpackNativesPlugin) to archive app.asar.
But inside the electron process, we can use modules from this archive like normal modules: import { SomeClass } from "app.asar/node_modules/some_module"
So the main idea is to tell utilityProcess where the archived node_modules are located (app.asar/node_modules).
Two functions enableASARSupport and removeGlobalNodeModuleLookupPaths did the job.
In our case the function enableASARSupport must inject path app.asar/node_modules before app.asar.unpacked/node_modules.
So utilityProcess module lookup paths first of all will look at app.asar/node_modules and then as normal lookup.
Save path where unpacked node_modules is located to modulesPath:
// my.worker.tsconstpath=require('path');constfs=require('fs');constModule=require('module');// increase number of stack frames(from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)Error.stackTraceLimit=100;constremoveGlobalNodeModuleLookupPaths=function(){if(typeofprocess?.versions?.electron==='string'){return;// Electron disables global search paths in https://github.com/electron/electron/blob/3186c2f0efa92d275dc3d57b5a14a60ed3846b0e/shell/common/node_bindings.cc#L653}// TODO@esm this might not work anymore (https://github.com/microsoft/vscode/issues/226042)constModule=require('module');// @ts-ignoreconstglobalPaths=Module.globalPaths;// @ts-ignoreconstoriginalResolveLookupPaths=Module._resolveLookupPaths;// @ts-ignoreModule._resolveLookupPaths=function(moduleName,parent){constpaths=originalResolveLookupPaths(moduleName,parent);if(Array.isArray(paths)){letcommonSuffixLength=0;while(commonSuffixLength<paths.length&&paths[paths.length-1-commonSuffixLength]===globalPaths[globalPaths.length-1-commonSuffixLength]){commonSuffixLength++;}returnpaths.slice(0,paths.length-commonSuffixLength);}returnpaths;};};/** * Helper to enable ASAR support. */constenableASARSupport=function(){// ESM-comment-beginconstNODE_MODULES_PATH=process.env.NODE_MODULES_PATH;constNODE_MODULES_ASAR_PATH=process.env.NODE_MODULES_PATH.replace('.unpacked','');// @ts-ignoreconstoriginalResolveLookupPaths=Module._resolveLookupPaths;// @ts-ignoreModule._resolveLookupPaths=function(request,parent){constpaths=originalResolveLookupPaths(request,parent);if(Array.isArray(paths)){for(leti=0,len=paths.length;i<len;i++){if(paths[i]===NODE_MODULES_PATH){paths.splice(i,0,NODE_MODULES_ASAR_PATH);break;}}}returnpaths;};// ESM-comment-end};// Remove global paths from the node module lookup (node.js only)removeGlobalNodeModuleLookupPaths();// Enable ASAR in our forked processesenableASARSupport();// Your worker code here(()=>{constSomeClass=require('@some/package');SomeClass.start_long_running_job();})().catch((error)=>{console.log('ERROR',error);process.exit(1);});
Be careful not to use require/import any packages from node_modules before the function enableASARSupport call, because Error MODULE_NOT_FOUND will raise.
Vite/webpack build can move require to the top of the code, so double-check it.
Activity
walmat commentedon Jun 25, 2019
Having the same issue with Electron 4.x.x and Node 12.4. Same behavior and setup. Have you found a fix for this?
I've tried:
export NODE_OPTIONS=--experimental-workerin both the runtime script && in my~/.bash_profileRUN_ELECTRON_AS_NODE=1flagI've switched to using a
global.window.Worker(web worker) for now, but would love to implement this lib in my project as soon as possible. If anyone can shed some light on this, or share a working example, that'd be great!edit: @sofianguy maybe you can lead us in the right direction, or give us a yes/no as to whether this is working in v4 or 5
d0peCode commentedon Jun 25, 2019
AFAIK Electron ship its own node fork, so your local 12.4 doesn't really matter.
No, still waiting for response. There is pinned discussion #18397 which might be relevant somehow.
infFenrir commentedon Jul 11, 2019
Having the same issue with Electron 5.0.6 and Windows 10 :(
App crash...
d0peCode commentedon Jul 16, 2019
Still no updates I still can not use threads in my electron app :(
infFenrir commentedon Jul 16, 2019
I tested it on - Electron 4, 5, 6 and even 7 ....
The result is the same, app crash :(
i7soft commentedon Jul 27, 2019
i have this problem too
d0peCode commentedon Aug 3, 2019
Is there any chance of some statement from electron team? It's been months now (half year #17030)
SaltFish001 commentedon Aug 11, 2019
Add NODE_OPTIONS(--experimental-worker) in your system environment variable.Restart terminal.
walmat commentedon Aug 11, 2019
@SaltFish001 And what happens when you try to spawn a worker?
SaltFish001 commentedon Aug 11, 2019
It's work.
SaltFish001 commentedon Aug 11, 2019
sorry, it's seems no working.The process ended and no error was reported
d0peCode commentedon Aug 15, 2019
I've tried it before even posting this issue. It doesn't change anything. Experimental flag for worker module was only required in node v11 if I remember correctly.
41 remaining items
User0111 commentedon May 22, 2021
This solution works for me as well
I was not able to use worker_threads with the following configuration:

I've used Electron Workers described here. Tasks processing is x2 faster now. I had a lot of
sexlove with Electron before I made it works but it is doable. it was really hard to get the list of all the dependencies(50) I have to add to asarUnpack. It's weird that so powerfull feature not documented well and needs so a lot of tricks to do. I would be happy to get know that I can do something simpler to make it works in another way even after all this hell.linonetwo commentedon May 23, 2021
@User0111 Actually I move to electron-forge, and use
threads-plugin, so webpack pack standalone js file for worker to run, and it works fine and no asar-unpack now.https://github.com/tiddly-gittly/TiddlyGit-Desktop/blob/afacc142f65b2afd951edd25ee4b731b6da3ad3f/webpack.plugins.js#L9-L41
https://github.com/tiddly-gittly/TiddlyGit-Desktop/blob/afacc142f65b2afd951edd25ee4b731b6da3ad3f/src/services/wiki/index.ts#L86
But it takes me a long time refactor my code to electron-forge + typescript.
And maybe it is because I upgraded to
[email protected]?m4heshd commentedon Jun 24, 2021
Electron really need to add more support for
worker_threadsespecially since it's a god given for GUI development. I still find it very weird why the worker is unable to use Electron's patchedfs. The most of my time I've spent debugging my projects, is related to Electron + asar + worker threads. There's always a "gotcha" waiting around the corner when going into production. @linonetwo I see that you're going through the same exact hell as I am since I see you on everyworker_threadsrelated issue that I'm also having.linonetwo commentedon Jun 24, 2021
@m4heshd Yes, but I finally make it worked... My latest solution includes:
m4heshd commentedon Jun 24, 2021
That's great. Unfortunately I cannot afford the time for any major refactoring right now. I'm just using my workers and dependencies unpacked for the moment. But love to move to a solid solution in the near future.
Coukaratcha commentedon Nov 30, 2021
@linonetwo Thank you for your help. I have used the asar.unpacked trick and it worked like a charm. However, I am very disappointed that
worker_threadsare not working properly out of the box when the app is packaged.I see that you found a better solution with electron-forge but still I think it should be great that electron improves
worker_threadsintegration. I am not satisfied with @codebytere's reply as I think that the issue was misunderstood: the issue is not thatworker_threadsare not working in renderer process, but that it cannot be possible to load modules from worker when usingworker_threadsfrom the main process. I think unpacking modules, even if it is a clever idea and a good temporary workaround, is not a good solution as we lost the perks of packaging.Is there any better solution if we want to keep nsis build target?
JaspreetChhabra commentedon May 21, 2022
@linonetwo Thanks for the help. The mentioned solution did work but my worker is also using 'electron-store' which internally uses 'electron' Module. 'electron' needs to be mentioned in the dev-dependecies and I am not able to unpack the modules mentioned in the dev-dependencies using "asarUnpack" in package.json. Any workaround for this ?
I am getting below error
`error Error: Cannot find module 'electron'
Require stack:
linonetwo commentedon May 21, 2022
@JaspreetChhabra I think code in worker don't have access to electron modules, because electron module only accessible from electron main thread (not any worker thread).
A workaround may be passing signal to trigger things in main thread and return data back to the worker thread. You may use threads plugin to simplify this, it supports rxjs, so passing things here and there is not that terrible.
@Coukaratcha I'm currently not unpacking everything, I only unpack as need. https://github.com/tiddly-gittly/TidGi-Desktop/blob/master/docs/Development.md#how-to-add-dependency-that-used-in-a-worker_thread
https://github.com/tiddly-gittly/TidGi-Desktop/blob/7831fa44bd27c18e1c25e62d78383d88c2930528/scripts/afterPack.js#L35-L50
It makes building complex a bit, but it works and not increasing too much bundle size.
well... 74M after install, but when zipped, no much , I think... See https://github.com/tiddly-gittly/TidGi-Desktop/releases TidGi-darwin-x64-0.7.7.zip 119 MB
Cmaxd commentedon Apr 17, 2024
@linonetwo thanks for your solution. do you know if electron has made any progress on this matter? It has been two years
linonetwo commentedon Apr 17, 2024
I don't think electron will do anything new on this, because worker_thread is working with simple npm dependency.
It depends on the npm package, if it use
fsandpathto locate other file, you have to move them to resource/ folder in afterPack script. I'm always doing this.But I'm not sure now, because @giladgd say it is possible to locate
.nodedependency without manually move them? withcatai/node-llama-cpp#199 (comment)I'm currently only move
.nodefiles to resource/ folder in afterBuild.js, and other JS dependency are bundled by electron-forge and works correctly in worker_thread.Cmaxd commentedon Apr 17, 2024
@linonetwo Thanks for reply. In my case, I have an SDK, the SDK calls a .dll based on ffi-napi, I configured SDK externals in webpack, and move the SDK to asar.unpacked folder, which runs well in main thread;
But now I want to call this SDK in worker_threads, it report an error MODULE_NOT_FOUND, to solve this problem, I need to bundle the SDK and worker's JS code together? and move the .node file in SDK's dependencies(ffi-napi ... ) to asar.unpacked folder ?
galetahub commentedon Aug 23, 2024
The main reason is that utilityProcess doesn't know where to find node_modules.
With option
asar: trueelectron package all of them (except native modules see AutoUnpackNativesPlugin) to archive app.asar.But inside the electron process, we can use modules from this archive like normal modules:
import { SomeClass } from "app.asar/node_modules/some_module"So the main idea is to tell utilityProcess where the archived node_modules are located (
app.asar/node_modules).I have found the solution in the VSCode source code:
https://github.com/microsoft/vscode/blob/16bb31f856d64fded303542a7e4a628dc92e63a2/src/bootstrap-node.js
Two functions enableASARSupport and removeGlobalNodeModuleLookupPaths did the job.
In our case the function enableASARSupport must inject path
app.asar/node_modulesbeforeapp.asar.unpacked/node_modules.So utilityProcess module lookup paths first of all will look at
app.asar/node_modulesand then as normal lookup.Save path where unpacked node_modules is located to modulesPath:
Pass variable modulesPath to our utilityProcess worker via envs:
Configure forge.config.js to include the worker module in the unpack:
Use hook to lookup
app.asar/node_modules:Be careful not to use require/import any packages from node_modules before the function enableASARSupport call, because Error MODULE_NOT_FOUND will raise.
Vite/webpack build can move
requireto the top of the code, so double-check it.Hope, this will help somebody