memfs icon indicating copy to clipboard operation
memfs copied to clipboard

Loading worker thread js file while using `patchRequire` and `patchFs` failed

Open mn4367 opened this issue 11 months ago • 0 comments

This is more a question than an issue.

I've created and saved a binary snapshot from a directory with a bunch of JavaScript code. After deleting this source code directory I'm loading the snapshot into a memory filesystem which then got patched with patchRequire. In the next step I created a union file system from the memory filesystem together with the regular node fs. In the last step I patched this union filesystem with patchFs. That worked without any problems so far and enabled me to load and execute an app from a single binary file without the original source code.

Then I modified the source code so that it makes use of a worker thread which is also contained in the binary snapshot file. This results in the following error:

node:internal/event_target:1096
  process.nextTick(() => { throw err; });
                           ^
Error: Cannot find module '/Users/johndoe/repos/test_memfs/WorkerThread.js'
    at Function._resolveFilename (node:internal/modules/cjs/loader:1142:15)
    at Function._load (node:internal/modules/cjs/loader:983:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:186:26)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:822:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28) {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Tested on Node.js v21.7.1 with fs-monkey 1.0.5, memfs 4.7.7 and unionfs 4.5.1. The test machine is a Mac with M1 chip running Ventura 13.6.4.

Can somebody confirm that it is impossible to load a worker thread source code file from a memory filesystem? Maybe there are any workarounds?

mn4367 avatar Mar 13 '24 18:03 mn4367