Search code examples
node.jselectronpuppeteerweb-workerworker

Using webworker inside electron app which use puppeteer


I read web workers API and puppeteer docs about worker. Now I'm trying to use worker within my electron app which use puppeteer webdriver. App was generated with vue-electron boileplate. I want to use worker in node side of electron application.

So far I tried like they show in MDN:

const Worker = require('worker_threads');
console.log(Worker);

this cause error:

Error: Cannot find module 'worker_threads'

I made sure I'm using latest node version by node -v in terminal and I am, version is v12.3.1.

Next I tried to dig something in nodejs / help on github I found this where they suggest to use --experimental-worker. So I did:

"dev": "node --experimental-worker .electron-vue/dev-runner.js",

Still same error. Next I tried to find something in electron issuses on github and I found this topic where they suggest to set environment variable of NODE_OPTIONS=--experimental-worker. So I did (windows 10):

enter image description here

(sorry about polish). Error is still same. I can't find any more tips in network. I definitely need your help guys.

I really want to try this worker_threads because every 300ms I'm getting very big (over 200k elements) array from one of pages and I'm performing filters on this array which cause browser "lags".

Environment:

  • Windows 10

Solution

  • So basically the Worker Thread API just shipped with node versions above v10. The version you're using ( electron 2.x ) ships with node v8.x which doesn't have the feature shipped.