[node.js] Need some help with worker threads

Hi,
I’m setting up a server and i’m trying to use working threads in it, for some reasons, I can’t use the the same file for the main thread and the worker thread (pass __filename at new Worker), so I’m trying the triple` approach, but I cant get the result I want:



//1.working properly: the output is { workerMsg: [ 1, 2, 3 ] }

if (isMainThread) {

const worker = new Worker(__filename, { workerData: [1,2,3] });

worker.on('message', message => console.log(message));

worker.postMessage('msg');

} else {

const { parentPort,workerData} = require('worker_threads');

parentPort.once('message',

message => parentPort.postMessage({ workerMsg: workerData }));

}

//2.doesn't work : the output is { workerMsg: undefined }.

const worker = new Worker(

``` //makes a "new file" and run it with eval:true

const { parentPort ,workerData} = require('worker_threads');

parentPort.once('message',

message => parentPort.postMessage({ workerMsg: workerData }));

```//end of "new file"

,{eval:true}, { workerData: [1,2,3] });

worker.on('message', message => console.log(message));

worker.postMessage('msg');


The expected result (the output at the 1st part of the code): { workerMsg: [ 1, 2, 3 ] }. The actual output (the output of the 2nd part of the code): { workerMsg: undefined }

How can I use the second code and make the workerData work?

The expected result (the output at the 1st part of the code): { workerMsg: [ 1, 2, 3 ] }. The actual output (the output of the 2nd part of the code): { workerMsg: undefined }

How can I use the second code and make the workerData work?