N
Nuxt2mo ago
tpluscode

Logging in server route

I wanted to log in server routes but trying to import @nuxt/kit throws
[nitro] ERROR RollupError: [plugin impound] This module cannot be imported in server runtime. [importing @nuxt/kit from server/api/showcases.post.ts]
[nitro] ERROR RollupError: [plugin impound] This module cannot be imported in server runtime. [importing @nuxt/kit from server/api/showcases.post.ts]
is there a built-in logger on server?
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @tpluscode. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- You cannot import @nuxt/kit in server routes, plugins, or runtime code. Nuxt Kit utilities like useLogger are only available for use inside Nuxt modules (i.e., in your module.ts or similar), not in runtime files such as server routes, composables, or plugins. Attempting to import @nuxt/kit in a server route will throw an error, as you have experienced:
This module cannot be imported in server runtime. [importing @nuxt/kit from server/api/showcases.post.ts]
This module cannot be imported in server runtime. [importing @nuxt/kit from server/api/showcases.post.ts]
Nuxt Kit usage docs For logging inside server routes, you can use the standard console.log, console.info, console.warn, or console.error methods. There is no special built-in logger for server routes documented in the official Nuxt docs. The useLogger utility is only for module development, not for runtime/server code. Example:
// server/api/hello.ts
export default defineEventHandler((event) => {
console.log('Request received:', event)
return { hello: 'world' }
})
// server/api/hello.ts
export default defineEventHandler((event) => {
console.log('Request received:', event)
return { hello: 'world' }
})
Server directory structure and usage In summary:
- Use console.log for logging in server routes.
- @nuxt/kit and its utilities are not available in runtime/server code.
- There is no alternative built-in logger for server routes in the knowledge sources. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@555413648382623754> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?