question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
2xAAcommented, 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.

0reactions
2xAAcommented, 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.

https://github.com/vcync/modV/commit/81eac0a13017f776af913449fad8c06a7c408821#diff-7b4e11be73603a3ee2c4d37bd49f053930d6056507a005e5639cea7c15686d5fR7-R14

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS modules not supported in Electron Web Workers
This error leads me to believe that Electron NodeJS modules are not supported in web workers. The Electron documentation here says otherwise, ...
Read more >
Multithreading | Electron
Any native Node.js module can be loaded directly in Web Workers, but it is strongly recommended not to do so. Most existing native...
Read more >
Why the Hell Would I Use Node.js? A Case-by-case Tutorial
Node.js can solve I/O scaling, but was not created to compute scaling problems. Learn why and when to use Node.js in this case-by-case...
Read more >
Cluster | Node.js v19.3.0 Documentation
A Worker object contains all public information and method about a worker. In the primary it can be obtained using cluster.workers . In...
Read more >
Node.js multithreading: Worker threads and why they matter
So, at this point, many people might think our solution should be to add a new module in the Node.js core and allow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found