With electron-vue boilerplate I generated electron application. Then I changed its electron version to 4.2.1
which ship node in version 10.11.0
.
Then I try to use worker_threads module. First I tried like this:
const Worker = require('worker_threads');
new Worker('./workerDummy.js');
but it says Worker is not a costructor so I consoled.log it:
{ isMainThread: true,
MessagePort:
{ [Function: MessagePort]
super_:
{ [Function: EventEmitter]
EventEmitter: [Circular],
usingDomains: false,
defaultMaxListeners: [Getter/Setter],
init: [Function],
listenerCount: [Function] } },
MessageChannel: [Function: MessageChannel],
threadId: 0,
Worker: [Function: Worker],
parentPort: null }
NodeJS docs about worker_threads is giving example with just new Worker(path)
but I assume it's other version or something and I should use Worker.Worker as I see .Worker
is a function. So I changed it to:
new Worker.Worker('./workerDummy.js');
and now application just crash without any error info.
I've created demo repository. Steps to reproduce:
git clone https://github.com/BorysTyminski/electron-worker-issue.git
- cd path/where/you/cloned
- npm install
- npm run dev
- click "Start NodeJS button"
creating NodeJS worker is in '@/src/main/index.js'.
I had some issues with electron-vue boilerplate before (for example I can not make it work with lates electron 5.x) so I wanted to check with some other boilerplate which is much more "pure" like electron-forge CLI which is recommended to use even in electron docs. So I generated fresh project with
npm i -g @electron-forge/cli
electron-forge init with-forge-cli --template=webpack
It has latest electron version 5.0.2
which ships node v12.x so it's fine. Then in main.js I added
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
and below in app.on ready:
app.on('ready', () => {
createWindow();
new Worker('./workerDummy.js');
});
behavior is exacly same so I doubt it's because of electron-vue.
I believe it's not usage error but electron bug so I posted issue on electron github.