Home > Enterprise >  Cannot import modules in worker script
Cannot import modules in worker script

Time:04-26

I'm new to using worker threads, thanks in advance for your help and time.

Using node v12.22.4, mongodb v4.2.19

What I'm trying to achieve is: on client-side, user asks for a big export/download operation. Server-side creates a "task" on db, acknowledges user request with a toaster task id, and then summons/creates a worker thread to do the computation. During computation, the status of task on db is being updated. Client received the task id, so he is able to ping the server to ask for the progress of the computation. And as soon as the work is done (status in db equivalent to done), client asks for result with a final request.

Here is how I summon a new worker from my 'export.controller.js' script:

const {Worker} = require('worker_threads');
const worker = new Worker("./server/api/export/export.worker.js", {execArgv : [...process.execArgv, '--unhandled-rejections=strict']});

The thing is, on my worker script ('export.worker.js'), I need to query collections (using mongodb) so I import the needed models. Models, I should mention, that I created.

But I get this error upon require line:

Error: Cannot find module 'components/model'
[1] Require stack:
[1] - path\server\models\api\api.model.js
[1] - path\server\api\export\export.worker.js
[1]     at Function.Module._resolveFilename    (internal/modules/cjs/loader.js:815:15)
[1]     at Function.Module._load (internal/modules/cjs/loader.js:667:27)
[1]     at Module.require (internal/modules/cjs/loader.js:887:19)
[1]     at require (internal/modules/cjs/helpers.js:74:18)
[1]     at Object.<anonymous> (path\server\models\api\api.model.js:3:24)
[1]     at Module._compile (internal/modules/cjs/loader.js:999:30)
[1]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
[1]     at Module.load (internal/modules/cjs/loader.js:863:32)
[1]     at Function.Module._load (internal/modules/cjs/loader.js:708:14)
[1]     at Module.require (internal/modules/cjs/loader.js:887:19)
[1]     at require (internal/modules/cjs/helpers.js:74:18)
[1]     at Object.<anonymous> (path\server\api\export\export.worker.js:5:14)
[1]     at Module._compile (internal/modules/cjs/loader.js:999:30)
[1]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
[1]     at Module.load (internal/modules/cjs/loader.js:863:32)
[1]     at Function.Module._load (internal/modules/cjs/loader.js:708:14) {
[1]   code: 'MODULE_NOT_FOUND',
[1]   requireStack: [
[1]     'path\\server\\models\\api\\api.model.js',
[1]     'path\\server\\api\\export\\export.worker.js'
[1]   ]
}

Raised with this line in my 'export.worker.js' script:

const Apis = require('../../models/api/api.model.js');

I should also mention that before, everything was handled on server-side directly, but by doing that I was unable to check on progress status of my computation. Hence the use of worker threads.

Thanks a lot.

CodePudding user response:

For anyone that has the same issue:

It was indeed related to paths. The module I was trying to import 'api.model.js' was referencing another module 'components/model' referencing another one... and so on - but all references without relative paths. I'm using webpack in my project so it worked well on one thread, but doesn't seem to do the trick with worker threads.

I eventually found my way out of it: I simplified my 'api.model.js' file so that it doesn't reference the 'components/model' file anymore. Another option would have been to replace every path with a relative one (my project is too big here and I didn't have the patience to do so). Good luck out there!

  • Related