pinojs/pino

"Module Resolution Error" when bundling Pino logger package

Opened this issue · 7 comments

Hello there,

I am working on a pino logger package for a NextJS application, structured with Turborepo and managed by PNPM. My bundling process involves esbuild-plugin-pino and tsup. For enhancing the development logging, I employ pino-pretty and have crafted a custom module to tailor the messageFormat function to my needs.

However, upon installing my logger package in my NextJS application, I encounter a series of errors that seem to stem from module resolution issues. Here's the detailed error trace:

`- error uncaughtException: Error: Cannot find module 'apps/web/.next/server/pages/api/lib/worker.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
at Module._load (node:internal/modules/cjs/loader:922:27)
... (truncated for brevity)
at MessagePort. (node:internal/main/worker_thread:164:24)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
... (truncated for brevity)
Error: the worker thread exited
at Worker.onWorkerExit (webpack-internal:///.../node_modules/.pnpm/thread-stream@2.4.1/node_modules/thread-stream/index.js:201:32)
... (truncated for brevity)

  • error ../../node_modules/.pnpm/thread-stream@2.4.1/node_modules/thread-stream/index.js (201:0) @ Worker.onWorkerExit
  • error uncaughtException: Error: the worker thread exited
    at Worker.onWorkerExit (webpack-internal:///.../node_modules/.pnpm/thread-stream@2.4.1/node_modules/thread-stream/index.js:201:32)
    ... (truncated for brevity)`

From my observations, keeping pino as a dependency in my package seems to trigger these issues. However, when I install pino in my NextJS app and specify it as a peerDep in my package, the errors are resolved. A similar resolution occurs when I install thread-stream directly in the app, although this workaround is not ideal.

I am looking for insights or recommendations on why this behavior occurs and how I might resolve it while keeping my package structure. For a hands-on look, I've set up a minimal repository to replicate the issue here.

Any guidance or suggestions would be greatly appreciated!

Thanks!

Update:

Looks like the package is working properly:

  • When using pino or thread-stream as peerDependencies
  • Or when using config.external.push({'thread-stream': 'commonjs thread-stream'}) in NextJs webpack config

Any ideas to make it work without peerDeps and extraconf on next.js.config side, thanks in advance :)

I can report the exact same behavior. When we refactored our logger into its own package (migrating to turbo repo) it kept working, but we also forgot to remove pino from our frontend app (nextjs) dependency list. After I fixed/removed the unnecessary dependency I got the same error and I can only get it to work again by using pino as a peerDependency.

The Problem doesn't exist with our node api. So I guess the problem is probably frontend related (nextjs/webpack)

I have absolutely no clue, but a PR/fix/doc update would be highly welcomed.

@victorlrz> * Or when using config.external.push({'thread-stream': 'commonjs thread-stream'}) in NextJs webpack config

peerDependencies approach did not work for me, neither serverComponentsExternalPackages

Webpack externals works great! - small typo though, should be: config.externals.push({ 'thread-stream': 'commonjs thread-stream' });

Is it related to pino packaging then?

This issue is spooky as there is no easy way to tell if it is related to pino and then no hint of how to fix it e.g.:
https://stackoverflow.com/questions/76617612/getting-uncaughtexception-error-cannot-find-module-next-server-app-home
vercel/next.js#50348
vercel/next.js#46987

I've been struggling with this for two days now. PNPM monorepo, Next 14.0.3.

Working solution for me right now:

  • Adding { 'thread-stream': 'commonjs thread-stream', pino: 'commonjs pino' } to webpack externals
  • Not adding anything serverComponentsExternalPackages (has no effect)
  • Both pino and thread-stream as peer deps

Haven't tried adding transports yet.

There are instructions about instructions about pino bundling here, but I had no luck with the webpack plugin.

A better guide/doc would be awesome to have.

I was having issues with tsup too, and tsup didn't seem to like esbuild-plugin-pino.
Following guidance here, I fixed by explicitly depending on pino & pino-pretty in my app package (which was getting bundled). Previously pino was a transitive dependency inside my logger package depended on by my app. Using pnpm which is strict about such things.