NextAuth throws ERR_INVALID_URL only after deploy
TypeError [ERR_INVALID_URL]: Invalid URLShort answer
In production NextAuth.js needs to know the site's own address. A missing or malformed NEXTAUTH_URL (it must be a full origin with https://), system environment variables switched off on Vercel, or a reverse proxy whose host header is not trusted all end in an invalid URL. Set the variable with its protocol and redeploy.
Fix it fast
1Find out which cause you have
List the auth variables the production process actually sees. A value without https:// or a missing name is the usual answer.
printenv | grep -E "^(NEXTAUTH_URL|AUTH_URL|AUTH_SECRET|NEXTAUTH_SECRET|AUTH_TRUST_HOST|VERCEL_URL)="2Match what you see, then jump to its fix
- 1
The value is example.com, localhost:3000 or empty, while development had no problem.NEXTAUTH_URL is missing in production or has no protocolFix - 2
You set no NEXTAUTH_URL on Vercel on purpose, expecting it to be detected, and production still fails.On Vercel, system environment variables are not exposedFix - 3
You use Auth.js v5 (AUTH_ variables) on your own server or in Docker behind nginx or a load balancer.Auth.js v5 sits behind a reverse proxy and does not trust the hostFix
- 1
What does the error look like?
TypeError [ERR_INVALID_URL]: Invalid URLSign-in works with npm run dev but the same code fails after deploy
Cause 1 of 3
NEXTAUTH_URL is missing in production or has no protocol
1Check
In next-auth v4 you set NEXTAUTH_URL to the canonical site URL when deploying, in the form of a full origin such as https://example.com. A bare host name cannot be parsed as a URL.
grep -n "NEXTAUTH_URL" .env.production .env 2>/dev/null2Fix
Set it in the host's environment settings, with the protocol, then redeploy.
NEXTAUTH_URL=https://example.com
NEXTAUTH_SECRET=<output of: openssl rand -base64 32>3Verify
Open /api/auth/providers on the live site; it should return JSON.
curl -s https://example.com/api/auth/providersConfirmed by next-auth.js.org
Cause 2 of 3
On Vercel, system environment variables are not exposed
1Check
The NextAuth.js docs say Vercel's system variables are detected automatically, provided Automatically expose System Environment Variables is checked in the Project Settings.
vercel env ls2Fix
Tick that option in Project Settings, Environment Variables, or set NEXTAUTH_URL yourself, then redeploy.
vercel env add NEXTAUTH_URL production3Verify
Redeploy and request the providers endpoint.
curl -s https://your-project.vercel.app/api/auth/providersConfirmed by next-auth.js.org
Cause 3 of 3
Auth.js v5 sits behind a reverse proxy and does not trust the host
1Check
Auth.js v5 infers the host from request headers. Behind a reverse proxy it must be told to trust X-Forwarded-Host. The docs infer this automatically on Vercel and Cloudflare Pages only.
printenv | grep -E "AUTH_TRUST_HOST|AUTH_URL"2Fix
Set AUTH_TRUST_HOST=true (or trustHost: true in the config), and set AUTH_URL only when the app lives under a different base path.
AUTH_TRUST_HOST=true
AUTH_SECRET=<output of: openssl rand -base64 33>3Verify
Restart the server and sign in through the public domain.
sudo systemctl restart my-appConfirmed by authjs.dev
How do I stop it coming back?
- Keep a production .env template listing NEXTAUTH_URL or AUTH_URL, and the secret, next to the code.
- Use a different NEXTAUTH_URL per environment instead of reusing the localhost value.
- After changing an environment variable, redeploy: running processes keep the old value.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
NextAuth logs NO_SECRET, or throws it in production
In production, we expect you to define a secret property in your configuration
NextAuth needs a secret to encrypt its JWT and hash tokens.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fix