Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

NextAuth throws ERR_INVALID_URL only after deploy

ErrorTypeError [ERR_INVALID_URL]: Invalid URL

Short 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.

  • Applies to next-auth 4, Auth.js 5, Next.js 15, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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)="
  2. 2Match what you see, then jump to its fix

    1. 1The value is example.com, localhost:3000 or empty, while development had no problem.NEXTAUTH_URL is missing in production or has no protocolFix
    2. 2You 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. 3You 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

What does the error look like?

  • TypeError [ERR_INVALID_URL]: Invalid URL
  • Sign-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/null

2Fix

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/providers

Confirmed by next-auth.js.org

Did this fix it?

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 ls

2Fix

Tick that option in Project Settings, Environment Variables, or set NEXTAUTH_URL yourself, then redeploy.

vercel env add NEXTAUTH_URL production

3Verify

Redeploy and request the providers endpoint.

curl -s https://your-project.vercel.app/api/auth/providers

Confirmed by next-auth.js.org

Did this fix it?

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-app

Confirmed by authjs.dev

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.