pino icon indicating copy to clipboard operation
pino copied to clipboard

Endless issues of Error: Cannot find module '....\.next\server\vendor-chunks\lib\worker.js'

Open jdoe802 opened this issue 1 year ago • 1 comments

have been working on this for a few weeks now. I was getting errors like the following when trying to implement pino transport

⨯ uncaughtException: Error: Cannot find module 'C:\Users\<redact>\.next\server\vendor-chunks\lib\worker.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at Module._load (node:internal/modules/cjs/loader:986:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:186:26)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:820:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28) {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
Error: the worker thread exited
    at Worker.onWorkerExit (webpack-internal:///(ssr)/./node_modules/thread-stream/index.js:201:32)
    at Worker.emit (node:events:519:28)
    at [kOnExit] (node:internal/worker:315:10)
    at Worker.<computed>.onexit (node:internal/worker:229:20)
    at Worker.callbackTrampoline (node:internal/async_hooks:130:17)
 ⨯ node_modules\thread-stream\index.js (201:0) @ Worker.onWorkerExit
 ⨯ uncaughtException: Error: the worker thread exited
    at Worker.onWorkerExit (webpack-internal:///(ssr)/./node_modules/thread-stream/index.js:201:32)
    at Worker.emit (node:events:519:28)
    at [kOnExit] (node:internal/worker:315:10)
    at Worker.<computed>.onexit (node:internal/worker:229:20)
    at Worker.callbackTrampoline (node:internal/async_hooks:130:17)

this originally seemed to be fixed with the following workaround being added to the next.config file: (the worker.js file, indexes file, and wait file were all copied from /node_modules/thread-stream/ folder)

function pinoWebpackAbsolutePath(relativePath) {
    console.log("relativepath:" + relativePath + " dirname:" +  __dirname);
    console.log(path.resolve(__dirname, relativePath));
    return path.resolve(__dirname, relativePath);
}

globalThis.__bundlerPathsOverrides = {
    'thread-stream-worker': pinoWebpackAbsolutePath('./worker.js'),
    'indexes': pinoWebpackAbsolutePath('./indexes.js'),
    'wait': pinoWebpackAbsolutePath('./wait.js'),
  };

however after further inspection some logs were outputting correctly with pino transport and others were causing this message

Error: Cannot find module 'C:\Users\<redact>\.next\server\vendor-chunks\lib\worker.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at Module._load (node:internal/modules/cjs/loader:986:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:186:26)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:820:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)
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:820:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)
    at MessagePort.callbackTrampoline (node:internal/async_hooks:130:17)
    at [kOnExit] (node:internal/worker:304:5)
    at Worker.<computed>.onexit (node:internal/worker:229:20)
    at Worker.callbackTrampoline (node:internal/async_hooks:130:17) {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

The logging transport that is triggering all these issues:

import pino, {Logger} from "pino";

export const masterLogger = pino({
    level: `${process.env.NEXT_PUBLIC_PINO_LOG_LEVEL ?? "debug"}`,
    redact: ['email', 'profileName', 'password', 'address'],
    //timestamp: () => `",timestamp":"${new Date(Date.now()).toISOString()}"`,
    transport: {
        target: 'pino-mongodb',
        options: {
            uri: process.env.MONGODB_URI,
            database: 'dev',
            collection: 'log-collection',
        },
    },
});

masterLogger.info('hello');

I've tried multiple solutions (commented out in the repo) Overall it is logging in some places but not in others in my full code repo. It logs to console in all files when I don't add the transport streams. After adding the transport streams and multiple workarounds, getting this. If next could just update their files so that worker.js was properly found in the first place many issues would be avoided.

jdoe802 avatar Aug 15 '24 16:08 jdoe802

The below did not work in production and I had to go back to a logger based on console.log.


I spent two days around the same issue, and subsequent ones.

After a lot of trial-and-error I figured out how to make pino work in a Next.js monorepo (Turborepo), with transports, pino-pretty, and functions for messageFormat, customPrettifier, and so on (i.e. non-streamable types).

Here is the setup, with explanations: https://github.com/vercel/next.js/discussions/46987#discussioncomment-10397449

Hope it helps (the key to the worker issue you are getting was the webpack part).

magnusriga avatar Aug 20 '24 15:08 magnusriga

I had a similar issue getting Next.js to work with Pino and was able to get it to work by setting the serverComponentsExternalPackages prop in the next.config.js file. I'm using pino-pretty for local development and a custom transport developed using "pino-abstract-transport".

module.exports = { experimental:{ serverComponentsExternalPackages: ['pino'] } };

richard-thai avatar Sep 19 '24 00:09 richard-thai

I had a similar issue getting Next.js to work with Pino and was able to get it to work by setting the serverComponentsExternalPackages prop in the next.config.js file. I'm using pino-pretty for local development and a custom transport developed using "pino-abstract-transport".

module.exports = { experimental:{ serverComponentsExternalPackages: ['pino'] } };

Excluding pino from the build does not solve the problem of using pino in middleware, server actions, and route handlers. Perhaps it would be possible to use just in normal server components, but not on the edge.

magnusriga avatar Oct 06 '24 12:10 magnusriga

Please reach out to the Next.js maintainers.

jsumners avatar Oct 07 '24 11:10 jsumners

I ran into similar but not exactly the same issue

NextJS pino Error: Cannot find module <local-repo-path>/.next/server/chunks/lib/worker.js

After significant digging, I realized that server-side logger, pino with logtail, is limited to server-only operations, and by mistake I was using the logger in client-side components. As pino uses worker threads from NodeJS, which is not available on client side, understandably build process was running into "Cannot find module".

So, I had to create two separate loggers - one using @logtail/pino for server-side and a second one using @logtail/browser for client side.

Later to make life easier, I just shifted to using @logtail/js which just works across both server and client.

Hope this info can help anyone else that falls into the trap and save hours of investigations.

avibathula avatar Oct 26 '24 22:10 avibathula

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[bot] avatar Nov 28 '24 00:11 github-actions[bot]