Native worker binary not found

I tried to integrate Mediasoup into a Typescript application built with webpack. Unfortunately the worker binary doesn’t get loaded when I am creating a new worker with the createWorker() method. The following error is produced:

Error: spawn /worker/out/Release/mediasoup-worker ENOENT

I tried to set PATH and LD_LIBRARY_PATH to …/node_modules/mediasoup/ or …/node_modules/mediasoup/worker/out/Release (where the binary in fact exists and is executable) but that didn’t help.

Any tips?

Thanks
Oliver

How are mediasoup (server side Node.js library) and webpack (JS client side library) related at all?

Webpack is not (only) client-side. It’s used as a kind of “Makefile” for the Typescript build process.Among other things it can also be used to create “dynamic imports” (implemented through “code splitting”).

I se, ok.

Honestly no idea about the issue. The mediasoup-worker is compiled and properly placed when you do npm install in your Node project that includes mediasoup dependency. And of course it’s placed within node_modules/mediasoup/ folder. I don’t think webpack can change that or expect something different.

What about that binary path that spawn() is using? It looks like an absolute path ("/worker/…"). Is that how it’s supposed to work?

I just tried on another system (Ubuntu) where I got the same problem but a more verbose error message:

Error: spawn /worker/out/Release/mediasoup-worker ENOENT
    at Process.ChildProcess._handle.onexit (node:internal/child_process:276:19)
    at onErrorNT (node:internal/child_process:476:16)
    at processTicksAndRejections (node:internal/process/task_queues:80:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn /worker/out/Release/mediasoup-worker',
  path: '/worker/out/Release/mediasoup-worker',
  spawnargs: [
    '--logLevel=warn',
    '--logTag=info',
    '--logTag=ice',
    '--logTag=dtls',
    '--logTag=rtp',
    '--logTag=srtp',
    '--logTag=rtcp',
    '--rtcMinPort=10000',
    '--rtcMaxPort=10100'
  ]
}

This is in mediasoup/src/Worker.ts (which is translated into mediasoup/lib/Worker.js):

// If env MEDIASOUP_WORKER_BIN is given, use it as worker binary.
// Otherwise if env MEDIASOUP_BUILDTYPE is 'Debug' use the Debug binary.
// Otherwise use the Release binary.
const workerBin = process.env.MEDIASOUP_WORKER_BIN
	? process.env.MEDIASOUP_WORKER_BIN
	: process.env.MEDIASOUP_BUILDTYPE === 'Debug'
		? path.join(__dirname, '..', 'worker', 'out', 'Debug', 'mediasoup-worker')
		: path.join(__dirname, '..', 'worker', 'out', 'Release', 'mediasoup-worker');

We don’t set any absolute path, or yes, we do (based on __dirname).

No idea how to help, you should debug what your “npm” installer is doing.

Thanks, that’s helpful. Looks like the problem is the build tool (webpack) somehow modifying __dirname.

There you have it: " webpack replaces __dirname with / . It’s a weird default and might cause some hard-to-find bugs."

https://codeburst.io/use-webpack-with-dirname-correctly-4cad3b265a92

now to find a solution … or use a different build tool.

Good catch.

BTW: read the description of the MEDIASOUP_WORKER_BIN environment variable above. It’s documented in mediasoup/doc/Building.md.