Replies: 6 comments 4 replies
-
Firstly, you can't use a POST method if you are not export async function GET(request: NextRequest) {
return new Response(JSON.stringify({example: "This is an example GET api endpoint"}))
} |
Beta Was this translation helpful? Give feedback.
-
Thank you, Theo-flux, appreciate your time. Interesting that page/api endpoints have no issue with the log statement. The log statement is causing the error with app/api endpoints and below both work fine once the log statement is removed: export async function GET(request: NextRequest) { OR export async function POST(request: NextRequest) { |
Beta Was this translation helpful? Give feedback.
-
More Details: Nextjs13 app/api endpoints error when using the pino logger: node:internal/event_target:1037
Endpoint does not call logger directly and calls service that uses the pino logger. Pino works fine with page/api endpoints. Anyone have any ideas how to get Pino Logger to work with nextjs13 app/api endpoints? |
Beta Was this translation helpful? Give feedback.
-
For what it's worth... I'm having the exact same problem with what appears to be a pretty straightforward setup. using pino to send logs to betterstack in react server components appears to be doa |
Beta Was this translation helpful? Give feedback.
-
I found a workaround to get pino logger working. Searched tons of stackoverflows to get the logger work on Nextjs +13, app routers, server components. Check deps first. "dependencies": {
"next": "13.5.4",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"pino": "^8.15.2"
} And then try below. import pino from 'pino'
// logger.ts
const streams = [
{ stream : process.stdout },
{ stream : pino.destination({
dest: yourLogPathHere,
append: true,
sync: true
})},
]
export const logger = pino({
timestamp: pino.stdTimeFunctions.isoTime,
base: undefined
}, pino.multistream(streams)) And then, call it in your API routes and app routers. export async function GET(request: Request) {
logger.info("name check: jake")
logger.warn("something serious going on here")
logger.error("bad bad things happened")
return NextResponse.json({
message: "hello jake"
})
} This will log it in terminal and write to file as well. |
Beta Was this translation helpful? Give feedback.
-
@u1x30 That is, you should change
|
Beta Was this translation helpful? Give feedback.
-
Page to Investigate
localhost:3000/api/example
Steps to Reproduce
Hi -
Running a simple route that outputs a log Hello World using latest version. Any idea on why getting following error?
INSTALL:
npm i next@latest react@latest react-dom@latest eslint-config-next@latest
CODE:
import {log} from '@/util/log-util'
import {NextRequest} from 'next/server'
export async function POST(request: NextRequest) {
log("Hello World")
}
ERROR:
error uncaughtException: Error: Cannot find module '/.next/server/app/api/example/lib/worker.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1072:15)
at Module._load (node:internal/modules/cjs/loader:925:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at MessagePort. (node:internal/main/worker_thread:185:26)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28) {
digest: undefined
}
at Worker.onWorkerExit (webpack-internal:///(sc_server)/./node_modules/thread-stream/index.js:163:34)
at Worker.emit (node:events:511:28)
at [kOnExit] (node:internal/worker:315:10)
at Worker..onexit (node:internal/worker:229:20) {
digest: undefined
}
null
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:261:61)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at ThreadStream.write (webpack-internal:///(sc_server)/./node_modules/thread-stream/index.js:193:25)
at Pino.write (webpack-internal:///(sc_server)/./node_modules/pino/lib/proto.js:162:12)
at Pino.LOG [as debug] (webpack-internal:///(sc_server)/./node_modules/pino/lib/tools.js:47:27)
at log (webpack-internal:///(sc_server)/./lib/util/log-util.ts:98:12)
at POST (webpack-internal:///(sc_server)/./app/api/example/route.ts:9:56)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:249:37) {
digest: undefined
}
Beta Was this translation helpful? Give feedback.
All reactions