Is there a way to run native code modules using worker threads?

Solution for Is there a way to run native code modules using worker threads?
is Given Below:

Is it possible to use native code modules on worker threads?

I am creating each worker thread as shown:

const { Worker } = require("worker_threads");
const auth = require("./auth.json");

const shardIds = [...Array(auth.shards).keys()];

for (let i = 0; i < shardIds.length; i++) {
    setTimeout(() => {
        new Worker("./bot.js", { workerData: { shardIds: [shardIds[i]], totalShards: auth.shards }});
    }, i * 5000);
}

…which appears to work just fine. Launching a single worker thread has no issues.

However, when multiple workers are created, this error is returned:

Error: Module did not self-register: '\?C:[file path]node_moduleserlpackbuildReleaseerlpack.node'.

This is a result of requiring the “erlpack” module on a second thread.

Having searched around a bit for solutions to this issue, it appears I’m not the only one who has had difficulty using modules which include native code on more than one thread.

I’ve tried rebuilding the modules, reinstalling the modules, and even requiring the module in the file which creates the workers and passing it in through the “workerData” (which did not work). I have also tried it on different devices, running different operating systems (Ubuntu and Windows) as well as setting the “win_delay_load_hook” option in the binding.gyp file for this module to “true”.

Unfortunately, I could not find any working solution.

Is it even possible to do this? Or is there another way to create multiple processes, while being able to pass data into each process, which also allows native code modules to be used?

The issue was with the module not being “context aware”.

This line:

NODE_MODULE

Had to be changed to:

NAN_MODULE_WORKER_ENABLED

…in the js/erlpack.cc file in order for it to work with threads.