Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a worker on electron doesn't include Node.js #53

Closed
Fransebas opened this issue Feb 8, 2020 · 9 comments
Closed

Creating a worker on electron doesn't include Node.js #53

Fransebas opened this issue Feb 8, 2020 · 9 comments
Labels
enhancement New feature or request needs more info

Comments

@Fransebas
Copy link

Hi, amazing library.
I'm having problems because when I create a simple worker like this one:

// node.js library
var child_process = require('child_process');

console.log("This is a testing string");

If I create the worker like this:

var worker1 = new Worker('./workerTest.js');

It works, but when I create the worker like this:

var worker2 = new Worker('./workerTest.js', { type: 'module' });

It shows this message Module not found: Error: Can't resolve 'child_process' meaning that the worker2 doesn't include Node.js. I don't know if I should include another option apart from type: 'module' or if this is not supported by this library.

Also, I can't use worker1 method because the real worker is more complex and has a lot of imports.

@Fransebas
Copy link
Author

I'm using electron": "^8.0.0 and node version v12.15.0

@developit
Copy link
Collaborator

developit commented Feb 21, 2020

Hiya - this module bundles Web Workers, not Node workers_threads. I think it might be possible to add an option for outputing worker_threads, but I worry that { type: 'module' } is misleading for that use-case, since Node does not actually have any plans to support a "type" option that I'm aware of.

FWIW it seems like in Electron you're getting a Web Worker. For a lot of use-cases, especially when you've already bundled source with Webpack, that might be a reasonable approach.

One thing you could try to get this working without any library changes would be to import the correct worker implementation yourself:

const { Worker } = require('worker_threads');

const worker = new Worker("./worker.js", { type: "module" });

@developit developit added enhancement New feature or request needs more info labels Feb 21, 2020
@developit
Copy link
Collaborator

I'm actually going to close this issue since it's a duplicate of #10 - we can continue discussion there!

@2xAA
Copy link

2xAA commented Dec 23, 2020

Hey @developit - I'm also facing this issue.
The scenario isn't to use worker_threads, it's to use a Worker with a NodeJS context in Electron:
https://www.electronjs.org/docs/tutorial/multithreading#multi-threaded-nodejs

My use-case is to use OffscreenCanvas within a Worker, which is impossible in a worker_therad as OffscreenCanvas is a browser API, not a NodeJS one. However I need access to NodeJS to use a native module (Electron docs say to avoid doing this, but so-far it has been successful).

I'm trying to migrate from worker-loader, but facing the same Module not found: Error: Can't resolve 'child_process' as @Fransebas had back in Feb.

@edenhermelin
Copy link

@2xAA did did you manage to workaround this issue? Im facing it too..

@2xAA
Copy link

2xAA commented Mar 4, 2021

@edenhermelin Unfortunately not. I wrote some pretty awful __dirname hacks around a library we were consuming to make it work with worker-loader.

Worked for a while but has broken with Electron 11 for some reason.

@rathboma
Copy link

rathboma commented Jun 1, 2021

I'm trying to solve this exact issue also.

@edenhermelin or @2xAA did either of you figure this out?

I also followed the directions in #75, which DOES let me create a worker from the main process, but not from the renderer process.

@rathboma
Copy link

rathboma commented Jun 1, 2021

@developit can we re-open this issue? It is not the same as creating a node worker.

What this is about:

  1. Creating a webworker
  2. That references node.js APIs
  3. That also references externalized native dependencies.

@2xAA
Copy link

2xAA commented Sep 20, 2021

@edenhermelin or @2xAA did either of you figure this out?

Nope, sorry.
This was my awful workaround for the library I was consuming. Might be quite specific, however.

vcync/modV@81eac0a#diff-7b4e11be73603a3ee2c4d37bd49f053930d6056507a005e5639cea7c15686d5fR7-R14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs more info
Projects
None yet
Development

No branches or pull requests

5 participants