NextAuth logs NO_SECRET, or throws it in production
[next-auth][warn][NO_SECRET]Short answer
NextAuth needs a secret to encrypt its JWT and hash tokens. In development it falls back to a generated value and only warns. In production there is no default, so the error is real. Generate a random string, set NEXTAUTH_SECRET (or AUTH_SECRET) on the server, and restart the app.
Fix it fast
1Find out which cause you have
Check whether the process that runs your app actually has the variable. Print only whether it is set, never the value.
node -e "console.log(Boolean(process.env.NEXTAUTH_SECRET || process.env.AUTH_SECRET))"2Match what you see, then jump to its fix
What does the error look like?
[next-auth][warn][NO_SECRET]In production, we expect you to define a secret property in your configuration
Cause 1 of 2
No secret is configured at all
1Check
Look for NEXTAUTH_SECRET or AUTH_SECRET in your env files and for a secret option in the NextAuth config.
grep -n "NEXTAUTH_SECRET\|AUTH_SECRET\|secret:" .env* app pages lib 2>/dev/null2Fix
Generate a random value and put it in .env.local. AUTH_SECRET is accepted as an alias for NEXTAUTH_SECRET.
openssl rand -base64 32
# .env.local
NEXTAUTH_SECRET=paste-the-generated-value-here3Verify
Restart the dev server; the warning should disappear.
npm run devConfirmed by next-auth.js.org
Cause 2 of 2
The secret is set locally but not on the production server
1Check
.env.local is for your machine. The deployed process needs the variable in its own environment, and Next.js reads .env files from the project root, not from src/.
ls -a | grep "^.env"
ls src 2>/dev/null | grep "^.env"2Fix
Add NEXTAUTH_SECRET to the environment of the service that runs the app (your host's env settings, or the process manager), then restart it.
NEXTAUTH_SECRET=paste-the-generated-value-here npm run start3Verify
Sign in on the deployed site and watch the logs stay clean.
npm run startConfirmed by nextjs.org
How do I stop it coming back?
- Add NEXTAUTH_SECRET to the deploy checklist and to your host's env settings.
- Use a different secret per environment and never commit it.
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
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 fixWeb apps
Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest failed to parse a file.
Jest only understands plain JavaScript until a transform and a few file mappings tell it more.
3 causes, each with a fix