pinojs / pino-debug

🌲high performance debug logging 🐞
MIT License
147 stars 19 forks source link

Use with ES Modules #36

Open reconbot opened 3 years ago

reconbot commented 3 years ago

I'm using esbuild on a project and pino-debug doesn't work because we're not using common js. I'm sure the exact same problem exists with webpack and maybe something similar with es modules in nodejs itself.

pino-debug modifies the module wrap which is super neat. However it's not used when bundling up the code base because the native require is never called. And it's possibly not used when using a es modules as there seems to be another way to do this kind of thing.

I'm pretty sure we could replace the debug package with a package of our choosing in the bundler approach. (I'm fuzzy on how.) I don't know what to do with native es modules.

Would solving this problem in this repo be welcome? I'd love to use pino-debug but if it's only for common js that's ok too.

mcollina commented 3 years ago

I would say that's only for commonjs for now. It might be possible to do this with a module loader.

jsumners commented 3 years ago

Does this not work?:

const pinoDebug = await import('pino-debug')
jmealo commented 2 years ago

@jsumners: const pinoDebug = (await import('pino-debug')).default works. The issue I'm seeing is with the conditional loading. In the event where NODE_ENV isn't development, an error is thrown that debug is called before pino-debug. If we use an async import we get pretty-printed (unredirected output) from avvio when using Fastify (but no throw). I'm not seeing a way to get the correct behavior I've had in the past with require and commonjs projects with ESM/Typescript.