401 Server error, no errors

Hi, My nuxt application simply throws a 401 error when built in production mode: '401 Server Error Request failed with status code 401' The logs, console or network requests show absolutely nothing failing. where should I look at this point.
11 Replies
kapa.ai
kapa.ai2w ago
Hi @ᴋʏʟʟɪᴀɴ. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- A 401 in production with “no errors” usually means something (middleware, plugin, proxy, or an external module) is denying the request before it reaches your page/component code. Based on the provided sources, here are the main places to check: 1. Auth / Supabase / route middleware - Some modules (e.g. Supabase) automatically redirect/guard routes and will send you to a login page or return 401 if the user isn’t authenticated, even if you didn’t add explicit redirects. [Supabase redirect] - Check: - Any defineNuxtRouteMiddleware in middleware/ that might call navigateTo('/login') or similar when user is missing. [Auth middleware] - Any auth-related modules (Supabase, VueFire, custom auth) and their redirect/guard options. 2. $fetch/useFetch interceptors and retry logic - If you’re using $fetch.create or a custom useFetch composable with onResponseError, they may be: - Treating all 401s as “auth failed” and redirecting/throwing. [Retry headers issue; Resend on 401] - Retrying with wrong headers or expired tokens so every request ends up as 401. [Retry headers issue] - Search your codebase for onResponseError, 401, or $fetch.create and log inside these hooks in production to see if they fire. 3. Backend / proxy behavior - If you’re proxying to an API in dev via vite.server.proxy, in Nuxt 4.2+ this must be moved to nitro.devProxy, otherwise behavior changes between dev and prod. [Proxy change] - Some users reported all proxied requests becoming 401 after upgrading. [Proxy change] 4. Server middleware / Nitro errors - Fatal errors thrown in middleware can be surfaced as error pages; non‑fatal ones can be passed to the router. [Middleware errors] - Check server/middleware/ and any auth checks there that might return 401 without logging. 5. SSR vs client behavior - Some redirects or errors only occur on the server side, making them hard to spot in browser devtools. In Nuxt 3, navigation in SSR must be done carefully (e.g. within useAsyncData or directly on the server response). [SSR redirect on 401] - You can add logging on the server (Nitro logs, console.log in middleware/handlers) to see which handler is returning 401. Because your logs “show nothing failing”, I’d suggest: 1. Search for any 401‑related code: - In middleware/, plugins/, and composables (onResponseError, navigateTo('/login'), throw showError({ statusCode: 401 }), etc.). [Multiple API requests] 2. Enable server-side logging around possible culprit areas: - Add temporary console.log('Reached X') in: - route middleware - server routes (server/api/*) - $fetch/useFetch interceptors 3. Hit the same URL directly in a tool like curl or Postman: - If you see 401 there as well, it’s almost certainly your backend or proxy auth, not Nuxt’s client code. The knowledge sources don’t contain a single “401 with no logs” Nuxt bug; they point instead to auth middleware, interceptors, or proxy configuration as the usual causes. If you can share your middleware/, $fetch plugin, or module list, it would be possible to narrow it down further. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@166602211780984832> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
ᴋʏʟʟɪᴀɴ
ᴋʏʟʟɪᴀɴOP2w ago
god lets just disable logging everywhere in nuxt so if something like this happens youre left clueless
danielroe
danielroe2w ago
probably you are missing an environment variable or token
ᴋʏʟʟɪᴀɴ
ᴋʏʟʟɪᴀɴOP2w ago
yeah u might be right lol im pretty sure im loading them in though even doing this: source .env && node .output/server/index.mjs @kapa.ai is running source .env && node .output/server/index.mjs the way to go, because nuxt doesnt recognize my environment variables
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Not necessarily. Nuxt is supposed to load environment variables for you; needing source .env && node .output/server/index.mjs usually means something else is wrong. From the docs and Q&As: - Nuxt 3 has built‑in dotenv support during nuxi dev, nuxi build and nuxi generate. A .env at the project root, with lines like KEY=VALUE, is automatically loaded and available in nuxt.config and at build time. [Nuxt .env support] - For runtime config in Nuxt 3, Nuxt also maps environment variables by name convention: - NUXT_PUBLIC_FOOruntimeConfig.public.foo - NUXT_BARruntimeConfig.bar
So using process.env.NUXT_PUBLIC_API_BASE inside runtimeConfig.public works reliably in all environments, including Heroku. [Runtime env mapping] Examples from the sources:
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: {
public: {
apiBase: process.env.NUXT_PUBLIC_API_BASE,
},
},
})
// nuxt.config.ts
export default defineNuxtConfig({
runtimeConfig: {
public: {
apiBase: process.env.NUXT_PUBLIC_API_BASE,
},
},
})
and in .env:
NUXT_PUBLIC_API_BASE=<https://example.com/v1/>
NUXT_PUBLIC_API_BASE=<https://example.com/v1/>
This fixed the user’s issue where Heroku was not picking up the right URL. [Runtime env mapping] For Docker / cloud: - You usually don’t mount .env and source it manually. Instead, pass environment variables via the container’s environment: section (docker‑compose) or platform config, and then read them with process.env in nuxt.config or via the NUXT_ / NUXT_PUBLIC_ convention. [Docker env in Nuxt] If nuxt “doesn’t recognize” your env vars, the likely issues (per sources) are: 1. .env is not in the project root or not in KEY=VALUE format. [Nuxt .env support] 2. In Docker / production, the env vars are not actually set in the container; mounting .env as a file is not enough—use environment: or platform‑specific env injection. [Docker env in Nuxt] 3. Wrong variable names for runtime config (e.g. using API_URL instead of NUXT_PUBLIC_API_BASE when you expect it in runtimeConfig.public.apiBase). [Runtime env mapping] So: you can run source .env && node .output/server/index.mjs as a workaround locally, but the recommended way is to: - Put .env at the project root for dev/build, and - Provide real environment variables (NUXT_* / NUXT_PUBLIC_*) in your production environment, then read them via runtimeConfig or process.env in nuxt.config. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@166602211780984832> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
ᴋʏʟʟɪᴀɴ
ᴋʏʟʟɪᴀɴOP2w ago
aha the docs show i can just do lorem: '' @danielroe could u enlighten me here
runtimeConfig: {
public: {
storeID: ''
},
},
runtimeConfig: {
public: {
storeID: ''
},
},
works fine in dev builds, but breaks on production builds. I'm using NUXT_PUBLIC_STORE_ID in my .env file. Then when using:
runtimeConfig: {
public: {
storeID: process.env.NUXT_PUBLIC_STORE_ID,
},
},
runtimeConfig: {
public: {
storeID: process.env.NUXT_PUBLIC_STORE_ID,
},
},
It works fine in both, am I misunderstanding the documentation here?
danielroe
danielroe2w ago
the first example should work fine in production. not sure why it isn't for you.
ᴋʏʟʟɪᴀɴ
ᴋʏʟʟɪᴀɴOP2w ago
perhaps its expecting NUXT_PUBLIC_STORE_I_D instead of NUXT_PUBLIC_STORE_ID
danielroe
danielroe2w ago
hm. I don't think it should.

Did you find this page helpful?