Next.js Discord

Discord Forum

Unable to use node modules in worker_threads worker

Unanswered
Chestnut-backed Chickadee posted this in #help-forum
Open in Discord
Chestnut-backed ChickadeeOP
I have a worker which I am calling from an api route. But I'm not able to use installed modules within the worker.

the code is like so:
// workers/extractContentWorker.js
const { parentPort, workerData } = require("node:worker_threads");
const { JSDOM } = require("jsdom");
const { Readability } = require("@mozilla/readability");

// use Readability in the below function
async function extractContent(url: string){
// implementation
}

async function run() {
  const { url } = workerData;
  const article = await extractContent(url);
  parentPort.postMessage(article);
}

run();

but its not able to find the module

Error generating HTML: Error: Cannot find module '@mozilla/readability'
Require stack:
- /var/task/workers/extractContentWorker.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)
    at Module._load (node:internal/modules/cjs/loader:984:27)
    at Module.require (node:internal/modules/cjs/loader:1231:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/var/task/workers/extractContentWorker.js:4:25)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Module._load (node:internal/modules/cjs/loader:1022:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/var/task/workers/extractContentWorker.js' ]
}


p.s: I have installed the dependencies properly

1 Reply

Chestnut-backed ChickadeeOP
P.P.S: It works locally, not able to get it to work on vercel