I've written a small program using Playwright and I'm trying to make it use worker_threads. Now I've written a test for it but for some reason I'm getting the above error in my test.
I tried tweaking the tsconfig settings, but that also didn't work. What I read most about is using commonjs as the module, which I'm doing. I also installed ts-node, but that also didn't do anything.
Below is the function I wrote to create a worker.
import { Worker } from "worker_threads";
const create_worker = (file: string, tasksData: {}) =>
new Promise((resolve, reject) => {
const worker = new Worker(file, {
workerData: tasksData,
});
worker.on("message", (data) => {
resolve(data);
});
worker.on("error", (msg) => {
reject(`An error occurred: ${msg}`);
});
});
export default create_worker;
And below is my test.
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
const search_grid = await construct_search_grid();
const chunks = chunkify_array(search_grid, 4);
const workerPromises: unknown[] = [];
for (let i = 0; i < 5; i ) {
const worker = await create_worker(
"./src/adapters/playwright.ts",
{
search_grid: chunks![i],
}
);
workerPromises.push(worker);
}
const thread_results = await Promise.all(workerPromises);
Now I believe everything checks out so I'm not really sure what to look for now. Does anyone know?
CodePudding user response:
It feels like you don't have tsconfig.json "module" configured?
I have mine set to "commonjs". Without it, I also get that same error.
CodePudding user response:
I was able to overcome this error by following this guide: https://wanago.io/2019/05/06/node-js-typescript-12-worker-threads/