Requiring file in webworker-threads nodejs multithreading

Viewed 501

I'm using https://github.com/audreyt/node-webworker-threads library for nodejs multithreading.

As stated in Webworker-threads in NodeJS I cannot just reference a function in the same file, I have to load the worker job with the library utilities.

I'm using ES6 and babel transpiler and run my project with npm.

So I have this:

// index.js
const startWorker = () => {

    console.log('creating worker:');

    const myWorker = new Worker (

        () => {
            console.log('in worker');

            self.importScripts('workerJob.js');

            self.postMessage('done');
        }

    );

    myWorker.onmessage = (event) => {console.log(event.data);};

};


//workerJob.js
console.log('worker job');

When I comment self.importScripts it works ok and print's out 'done' message, but when I try to import with self.importScripts('workerJob.js') the program hangs. I also tried self.importScripts('./workerJob.js').

Program structure looks like this:

- root
  -  backend
     - src
         - index.js
         - workerJob.js
     - package.json

I also use nodemon to run this live.

How to import worker's job code?

0 Answers
Related