Next.jsでpino-prettyのtransportオプションを使うとworker threadsの問題でエラーになる。transportの代わりにstreamを使えば解決する。
Using the transport option of pino-pretty in Next.js results in an error due to worker threads issues. Using stream instead of transport resolves the problem.
Next.js 13以降のApp Routerで、pinoのtransportオプションを使ってpino-prettyを設定すると、以下のようなエラーが発生する:
When using the pino transport option to configure pino-pretty in Next.js 13 or later with the App Router, the following error occurs:
Error: the worker thread exitedpino-prettytransportオプションは内部でworker threadsを使用する。Next.jsのバンドラー(SWC/webpack)がこれらのモジュールを正しく処理できず、ランタイムでエラーになる。
thread-stream// ❌ これはNext.jsで動かない
// ❌ This doesn't work in Next.js
const logger = pino({
transport: {
target: 'pino-pretty',
options: { colorize: true }
}
})next.config.jsserverExternalPackagesに追加する方法も提案されているが、ビルドは通っても実行時にモジュールが見つからないケースがある。
Although adding serverExternalPackages to next.config.js has been suggested, there are cases where the module is not found at runtime even if the build succeeds.
transportではなくstreamを使う。 streamはworker threadsを使わないため、Next.jsでも問題なく動作する。
Use stream instead of transport. stream does not use worker threads, so it works fine with Next.js.
import pino from "pino";
import pretty from "pino-pretty";
// ✅ streamを使えばNext.jsでも動く
const stream = pretty({
colorize: true,
ignore: "pid,hostname",
translateTime: "SYS:HH:MM:ss.l",
});
const logger = pino({ level: "debug" }, stream);# 参考
# References
- [Pino Bundling Documentation]