Nuxt startup error when nitro `experimental.websocket: true`, even when not using them with nuxt-auth-utils
I'm using nuxt-auth-utils, but currently not with websockets (yet).
When websocket: true, whenever I first access auth in my nuxt app after starting it, I get this error:
ERROR / Application error: Cannot read properties of undefined (reading 'runtimeConfig')
at useRuntimeConfig (node_modules/nitropack/dist/runtime/internal/config.mjs:17:27)
at _useSession (node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js:49:27)
at getUserSession (node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js:7:25)
at <anonymous> (node_modules/nuxt-auth-utils/dist/runtime/server/plugins/ws.js:5:11)
at node_modules/hookable/dist/index.mjs:48:66
at node_modules/hookable/dist/index.mjs:48:56
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Object.onRequest (node_modules/nitropack/dist/runtime/internal/app.mjs:49:7)
at async node_modules/h3/dist/index.mjs:1993:7
at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
ERROR /api/_auth/session Application error: Cannot read properties of undefined (reading 'runtimeConfig')
at useRuntimeConfig (node_modules/nitropack/dist/runtime/internal/config.mjs:17:27)
at _useSession (node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js:49:27)
at getUserSession (node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js:7:25)
at <anonymous> (node_modules/nuxt-auth-utils/dist/runtime/server/plugins/ws.js:5:11)
at node_modules/hookable/dist/index.mjs:48:66
at node_modules/hookable/dist/index.mjs:48:56
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Object.onRequest (node_modules/nitropack/dist/runtime/internal/app.mjs:49:7)
at async node_modules/h3/dist/index.mjs:1993:7
at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
My nuxt.config.ts:
export default defineNuxtConfig({
experimental: {
websocket: true
},
modules: ['nuxt-auth-utils'],
})
If I set websocket: false, the problem goes away.
Update
- The issue is only present on the initial (internally generated by nuxt-auth-utils?) calls to
/api/_auth/session - The issue is caused by event.context.nitro being absent during these calls
Following the chain of errors back, we find node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js, and in _useSession:
function _useSession(event, config = {}) {
if (!sessionConfig) {
// I've added a console.log here, to help understand what's happening
console.log(`in nuxt-auth-utils _useSession(): isEvent(event)=${isEvent(event)}, event.context.nitro=${event.context.nitro}`)
const runtimeConfig = useRuntimeConfig(isEvent(event) ? event : void 0);
// ...etc...
}
If useRuntimeConfig is called, it checks for:
if (event.context.nitro.runtimeConfig) {
return event.context.nitro.runtimeConfig;
}
Which causes an error because event.context.nitro is undefined in the first two requests _useSession sees.
When my nuxt app is freshly started, and I first access a page (I haven't untangled what that page might be doing, but my usage is very simple), I get:
✔ Vite client built in 23ms 1:59:59 AM
✔ Vite server built in 221ms 2:00:00 AM
✔ Nuxt Nitro server built in 447 ms nitro 2:00:00 AM
ℹ Vite client warmed up in 1ms 2:00:00 AM
Using model: anthropic/claude-3.7-sonnet (useCheapModel=false)
ℹ Vite server warmed up in 398ms 2:00:01 AM
Using vars defined in .dev.vars
in nuxt-auth-utils _useSession(): isEvent(event)=true, event.context.nitro=undefined
ERROR / Application error: Cannot read properties of undefined (reading 'runtimeConfig')
at useRuntimeConfig (node_modules/nitropack/dist/runtime/internal/config.mjs:18:27)
at _useSession (node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js:50:27)
at getUserSession (node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js:7:25)
at <anonymous> (node_modules/nuxt-auth-utils/dist/runtime/server/plugins/ws.js:5:11)
at node_modules/hookable/dist/index.mjs:48:66
at node_modules/hookable/dist/index.mjs:48:56
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Object.onRequest (node_modules/nitropack/dist/runtime/internal/app.mjs:49:7)
at async node_modules/h3/dist/index.mjs:1993:7
at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
in nuxt-auth-utils _useSession(): isEvent(event)=true, event.context.nitro=undefined
ERROR /api/_auth/session Application error: Cannot read properties of undefined (reading 'runtimeConfig')
at useRuntimeConfig (node_modules/nitropack/dist/runtime/internal/config.mjs:18:27)
at _useSession (node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js:50:27)
at getUserSession (node_modules/nuxt-auth-utils/dist/runtime/server/utils/session.js:7:25)
at <anonymous> (node_modules/nuxt-auth-utils/dist/runtime/server/plugins/ws.js:5:11)
at node_modules/hookable/dist/index.mjs:48:66
at node_modules/hookable/dist/index.mjs:48:56
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Object.onRequest (node_modules/nitropack/dist/runtime/internal/app.mjs:49:7)
at async node_modules/h3/dist/index.mjs:1993:7
at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
in nuxt-auth-utils _useSession(): isEvent(event)=true, event.context.nitro=[object Object]
Notice that the first two requests failed, because event.context.nitro was not set, and finally, the third time it was set. After this, I can reload browser, etc etc, and the error does not return until I kill nuxt dev and start it again.
Any ideas what I could check further to isolate this?
What's weird is I still get the 2x startup errors even if I comment out the only place in my app that directly calls: await requireUserSession(event).
OK, I've confirmed the two errors are see on startup are calls to /api/_auth/session, I believe (?) these are triggered automatically whenever using nuxt-auth-utils:
modules: ['nuxt-auth-utils'],
I believe this might be a timing issue: the idea is internal calls nuxt-auth-utils to /api/_auth/session are perhaps done too early, and with websocket enabled, it takes longer, and nitro hasn't bound to the event yet? Or something?
I can use requireUserSession(event) succesfuly in my own code. only the initial calls to /api/_auth/session fail:
// server/api/message.post.js
export default defineEventHandler(async (event) => {
console.log("event.context.nitro", event.context.nitro) // event.context.nitro exists
await requireUserSession(event) // no problem
})