pinojs/pino

Import error when using worker with pkg

Closed this issue · 1 comments

Hi there!

When using pkg (more specifically yao-pkg) the transport stream builder is unable to load files.
This is happening on node v20 while using CommonJS modules throughout the whole project, and it's throwing the following error:

TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:213:9)
    at eval (eval at <anonymous> (C:\snapshot\backend\node_modules\real-require\src\index.js:4:20), <anonymous>:3:1)
    at loadTransportStreamBuilder (C:\snapshot\backend\node_modules\pino\lib\transport-stream.js:28:19)
    at C:\snapshot\backend\node_modules\pino\lib\worker.js:14:22
    at Array.map (<anonymous>)
    at C:\snapshot\backend\node_modules\pino\lib\worker.js:13:39
    at start (C:\snapshot\backend\node_modules\thread-stream\lib\worker.js:61:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on ThreadStream instance at:
    at Immediate.<anonymous> (C:\snapshot\backend\node_modules\thread-stream\index.js:369:12)
    at process.processImmediate (node:internal/timers:478:21)

Node.js v20.11.1

This is essentially the same issue I encountered in the thread-stream package.

And just like in the other issue, adding the ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING error code here fixes the problem:

else if (error.code === undefined || error.code === 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING') {
      // When bundled with pkg, an undefined error is thrown when called with realImport
      fn = realRequire(decodeURIComponent(target))
}

I am working on creating a PR with the same fix as I did for thread-stream.

Go for it! Also add a test.