pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

Pino transports in combination with node 20 and `--import` throw an uncaught exception #1851

Open Raynos opened 11 months ago

Raynos commented 11 months ago

I have code like the following

  const loggerOptions: LoggerOptions = {
    level: (IS_TEST ? 'warn' : process.env.LOG_LEVEL || 'debug') as Level,
    transport:
      ENVIRONMENT === 'development' && !IS_TEST
        ? {
            target: 'pino-pretty',
            options: {
              colorize: true,
              translateTime: true,
            },
          }
        : undefined,
    base: baseFields,
    mixin: () => {
      return { taskArn };
    },
  };

This means i am conditionally using the pino-pretty transport only in development and I am using pino@8.7.0

This throw the following exception

importing the script /home/raynos/soundxyz/code/tools/admin-tools/src/scripts/reloadArweave.ts
bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)

node:internal/event_target:1083
  process.nextTick(() => { throw err; });
                           ^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/raynos/soundxyz/code/tools/shared/bob-tsm-hooks.cjs' imported from /home/raynos/soundxyz/code/tools/admin-tools/src/
    at new NodeError (node:internal/errors:406:5)
    at finalizeResolution (node:internal/modules/esm/resolve:233:11)
    at moduleResolve (node:internal/modules/esm/resolve:845:10)
    at defaultResolve (node:internal/modules/esm/resolve:1043:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:228:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:315:34)
    at node:internal/process/esm_loader:26:84
    at node:internal/per_context/primordials:544:39
Emitted 'error' event on Worker instance at:
    at [kOnErrorMessage] (node:internal/worker:326:10)
    at [kOnMessage] (node:internal/worker:337:37)
    at MessagePort.<anonymous> (node:internal/worker:232:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28) {
  url: 'file:///home/raynos/soundxyz/code/tools/shared/bob-tsm-hooks.cjs',
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v20.9.0
 ELIFECYCLE  Command failed with exit code 1.

I run my entire program with

TZ=UTC IS_ADMIN_TOOLS=1 node --import=../../shared/bob-tsm-hooks.cjs src/index.ts

The combination of a transport, a worker thread and the --import flag makes the worker thread itself throw an exception.

It's also because my code actually calls

const $dirname = new URL('.', import.meta.url).pathname;

inquirer.registerPrompt('autocomplete', inquirerPrompt);
process.chdir($dirname);

I imagine this process.chdir() also makes a mess of things.

I spend half an hour debugging it before i disabled the pino-pretty transport and identified this was the source of the uncaught.

mcollina commented 11 months ago

I'm not 100% sure how to fix this, this seems an alignment of conditions that cause the problem.

Any chance would you be up for a PR?