Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Pino swallows module errors when loading ESM #1989

Closed
sharifzadesina opened this issue Jun 12, 2024 · 2 comments · Fixed by #1990
Closed

Pino swallows module errors when loading ESM #1989

sharifzadesina opened this issue Jun 12, 2024 · 2 comments · Fixed by #1990
Labels

Comments

@sharifzadesina
Copy link

sharifzadesina commented Jun 12, 2024

When I use below syntax to configure Pino, I get the following error:

import pino from 'pino';
import { resolve } from 'node:path';

const dailyTransport = {
  target: resolve('rotating-file-transport.js')
}

var p = pino({
  transport: {
    targets: [dailyTransport]
  },
});

p.debug('test');

rotating-file-transport.js:

import rfs from 'rotating-file-stream';
import path from 'path';

export default function (options) {
  const { destination, ...opts } = options;
  const filename = path.basename(destination);
  const dirname = path.dirname(destination);
  return rfs.createStream(filename, { ...opts, path: dirname });
}

Error:

node:events:497
      throw er; // Unhandled 'error' event
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/sina/Projects/pouch-js-webrunner/rotating-file-transport.js from /home/sina/Projects/pouch-js-webrunner/node_modules/real-require/src/index.js not supported.
Instead change the require of rotating-file-transport.js in /home/sina/Projects/pouch-js-webrunner/node_modules/real-require/src/index.js to a dynamic import() which is available in all CommonJS modules.
    at realRequire (/home/sina/Projects/pouch-js-webrunner/node_modules/real-require/src/index.js:11:10)
    at loadTransportStreamBuilder (/home/sina/Projects/pouch-js-webrunner/node_modules/pino/lib/transport-stream.js:38:12)
    at async /home/sina/Projects/pouch-js-webrunner/node_modules/pino/lib/worker.js:82:18
    at async Promise.all (index 0)
    at async module.exports (/home/sina/Projects/pouch-js-webrunner/node_modules/pino/lib/worker.js:81:15)
Emitted 'error' event on ThreadStream instance at:
    at Immediate.<anonymous> (/home/sina/Projects/pouch-js-webrunner/node_modules/thread-stream/index.js:369:12)
    at process.processImmediate (node:internal/timers:478:21)

Node.js v20.14.0
@mcollina
Copy link
Member

The above code does not reproruce that error. However I did investigate anyway.

The underlying problem is the following:

file:///Users/matteo/Repositories/pino/aaa/rotating-file-transport.mjs:1
import rfs from 'rotating-file-stream';
       ^^^
SyntaxError: The requested module 'rotating-file-stream' does not provide an export named 'default'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:217:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadTransportStreamBuilder (/Users/matteo/Repositories/pino/lib/transport-stream.js:28:13)
    at async /Users/matteo/Repositories/pino/lib/worker.js:82:18
    at async Promise.all (index 0)
    at async module.exports (/Users/matteo/Repositories/pino/lib/worker.js:81:15)
    at async start (/Users/matteo/Repositories/pino/node_modules/thread-stream/lib/worker.js:63:17)
node:events:497
      throw er; // Unhandled 'error' event
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/matteo/Repositories/pino/aaa/rotating-file-transport.mjs not supported.
Instead change the require of /Users/matteo/Repositories/pino/aaa/rotating-file-transport.mjs to a dynamic import() which is available in all CommonJS modules.
    at realRequire (/Users/matteo/Repositories/pino/node_modules/real-require/src/index.js:11:10)
    at loadTransportStreamBuilder (/Users/matteo/Repositories/pino/lib/transport-stream.js:39:12)
    at async /Users/matteo/Repositories/pino/lib/worker.js:82:18
    at async Promise.all (index 0)
    at async module.exports (/Users/matteo/Repositories/pino/lib/worker.js:81:15)
    at async start (/Users/matteo/Repositories/pino/node_modules/thread-stream/lib/worker.js:63:17)
Emitted 'error' event on ThreadStream instance at:
    at Immediate.<anonymous> (/Users/matteo/Repositories/pino/node_modules/thread-stream/index.js:369:12)
    at process.processImmediate (node:internal/timers:478:21)

Node.js v20.13.1

However, pino has a bug and it swallows the error

@mcollina mcollina added the bug label Jun 12, 2024
@mcollina mcollina changed the title Require of ES Module not supported Pino swallows module errors when loading ESM Jun 12, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 13, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants