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.

unable to use web-worker with node integration enabled

See original GitHub issue

Describe the bug I have an Electron project in typescript, with webpack and of course - with vue-cli-plugin-electron-builder.

Im trying to add a new web worker to my project and Im having a hard time making everything work as expected. The web worker is written in TS, it should handle some long tasks in the background in order to let the UI thread run smooth.

I followed the guide in the documentation to use worker-plugin, but I’m getting the following error:

 INFO  Starting development server...
98% after emitting CopyPlugin

 ERROR  Failed to compile with 1 error                                                                                                                                                                                                        9:49:05 PM

 error  in ./src/app/workers/my-worker.ts

Syntax Error: ModuleNotFoundError: Module not found: Error: Can't resolve 'child_process' in '/Users/dev/workspaces/electron-project/src/shared'


 @ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/app/App.vue?vue&type=script&lang=js& 1:0-114
 @ ./src/app/App.vue?vue&type=script&lang=js&
 @ ./src/app/App.vue
 @ ./src/app/main-router.js
 @ ./src/app/main.js

Basically my webworker imports a file that use some nodeJS modules like path, fs and child_process, but it fails there. (there was also a weird prettier error when I tried to use worker-plugin, but I think it’s a non related issue with the worker plugin itself).

Before trying to use worker-plugin I also tried to use worker-loader, there I was able to use the nodejs modules but whenever I had an import to a 3rd party library I get another error which I couldn’t find anything relevant about how to fix it: image

webpack config ( note that I tried also the commented out method here)

module.exports = {
  entry: path.resolve(rootDir, './src/app/main.js'),
  resolve: {
    extensions: ['.js', '.ts', '.vue'],
  },
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
      // {
      //   test: /\.worker\.ts$/,
      //   use: [{ loader: 'worker-plugin/loader' }],
      //   exclude: /node_modules/,
      // },
    ],
  },
  plugins: [new WorkerPlugin()],
};

relevant browserWindow config image

my-worker.ts

import path from 'path';
import { convertFile } from '@/tasks/converter'; // This module imports a script that requires child_process

// Listen to messages from parent thread
addEventListener('message', async (event) => {
  console.log('Worker received message:', event.data);
  // Do some calculations and send the result back to parent thread
  const parsed = path.parse(event.data.path);
  console.log(`inside the worker:`, parsed);
  const convertedFile = await convertFile(event.data.content, convertedPath);
  // @ts-ignore
  self.postMessage({ result: convertedFile });
});

App.vue

...
          <!--Template Simple Code -->
          <h1 @click="someMethod">TestWorker</h1>
          <!--Template Simple Code -->
...
const worker = new Worker('../workers/my-worker', { type: 'module' });
...
  mounted() {
    worker.addEventListener('message', function (event) {
      console.log('Received data from worker:', event.data);
    });
  },
...
methods:{
someMethod() {
    console.log('sending to the worker');
      // Send some message to the worker
      worker.postMessage({ parse: '~/workspaces/test/file.txt' });
    },
}

dependencies

...
        "vue-cli-plugin-electron-builder": "^2.0.0-rc.6",
        "vue": "^2.6.12",
        "electron": "11.3.0",
        "electron-devtools-installer": "^3.1.1",
        "typescript": "^4.1.5",
        "ts-loader": "^8.0.17",
        "@babel/preset-typescript": "^7.12.17",
        "@typescript-eslint/eslint-plugin": "^4.15.1",
        "@typescript-eslint/parser": "^4.15.1",
        "@vue/cli-plugin-babel": "^4.5.11",
        "@vue/cli-plugin-eslint": "^4.5.11",
        "babel-eslint": "^10.1.0",
        "@vue/cli-service": "^4.5.11",
        "webpack": "^4.46.0",
        "webpack-cli": "^4.3.1",
        "worker-plugin": "^5.0.0"
        "untildify": "^4.0.0",
...

To Reproduce Steps to reproduce the behavior:

  • add a short TS web-worker that use fs / path / etc.
  • add workerPlugin to webpack config’s plugin
  • in your background.js allow nodeIntegration + nodeIntegrationInWorker
  • import the worker and call it from your App.vue

Expected behavior The worker should be accessible from the app 😄

Environment (please complete the following information):

  • custom config for vcp-electron-builder: Im using typescript in the project, enabling nodeIntegration and nodeIntegrationInWorker:
electronBuilder: {
  outputDir: electronOutputDir,
  mainProcessFile: 'src/app/background.js',
  mainProcessTypeChecking: false, 
  pages: {
    index: path.join(__dirname, './src/app/main.js'),
  },
  chainWebpackMainProcess: (config) => {
    for (const [key, value] of Object.entries(appWebpackConfig.resolve.alias)) {
      config.resolve.alias.set(key, value);
    }
  },
  nodeIntegration: true,
  nodeIntegrationInWorker: true,
...

btw - this plugin is awesome 😎

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
arsh09commented, Jul 3, 2021

@edenhermelin

It is okay. I found out that it is impossible to use node native modules in web workers if I call them in a front end process. I ended up using Electron IPC and spawning a new process which calls the DLL function instead.

Thank you though.

Cheers

0reactions
edenhermelincommented, Jul 3, 2021

Hi @arsh09 , sorry for the late response.

I’m not familiar with ref/ffi, but from reading a little about it - it looks like it’s using nativee modules (like @nklayman explained in a previous comment).

What I managed to do is to get a web worker working with fs / path, etc, but I didn’t use in my project any native modules of nodeJS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support Web Workers · Issue #43583 · nodejs/node - GitHub
I have yet to see reliable wrapper for worker_threads that implements an API sufficiently compatible with web workers. I'm glad to see people ......
Read more >
How to get node integration working in an Electron Web ...
Using require inside a worker works. I made a bare minimal example here: https://github.com/trusktr/electron-web-worker-example.
Read more >
Supporting Web Workers API in Node.js vs Just Using Deno
Does adding Web Workers API support to Node.js make sense given it ... not necessarily to be compatible with Web Worker API in...
Read more >
Webworker can't find Core Angular stuff it claims it needs, why?
I'm doing my first web worker in an Angular v8 project. I would like to have my web worker make HTTP service calls,...
Read more >
Security | Electron
It is paramount that you do not enable Node.js integration in any renderer ( BrowserWindow , BrowserView , or <webview> ) that loads...
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