Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
jdoe802 opened this issue Aug 15, 2024 · 5 comments

Comments

@jdoe802
Copy link

jdoe802 commented Aug 15, 2024

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.

@magnusriga
Copy link

magnusriga commented Aug 20, 2024

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: vercel/next.js#46987 (comment)

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

@richard-thai
Copy link

richard-thai commented Sep 19, 2024

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'] } };

@magnusriga
Copy link

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.

@jsumners
Copy link
Member

jsumners commented Oct 7, 2024

Please reach out to the Next.js maintainers.

@jsumners jsumners closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2024
@avibathula
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants