FuntranslatorCreate Fun Language Translations
Free
Back to Blog
2026-11-166 min read

How to Set JWT_SECRET in a .env File (Node.js, Docker, Production)

Generate a strong JWT_SECRET, store it in a .env file or secrets manager, and load it safely in Node.js, Docker and hosting platforms. Includes JWT_ACCESS_SECRET and refresh secret examples.

What JWT_SECRET is

JWT_SECRET is the environment variable most projects use to hold the key that signs JSON Web Tokens with an HMAC algorithm such as HS256. Anyone who has it can create tokens your server will accept, so it must be random, long and never committed to source control.

1. Generate a strong value

For HS256 use at least 32 random bytes (256 bits). Use the JWT secret key generator, or create one locally:

openssl rand -hex 32
openssl rand -base64 32
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

Do not use words, project names or values such as secret or changeme.

2. Put it in a .env file

# .env
JWT_SECRET=paste-your-generated-value-here
JWT_EXPIRES_IN=15m

Add .env to .gitignore before your first commit, and commit a .env.example with placeholder values instead.

3. Load it in Node.js

import jwt from 'jsonwebtoken';

const secret = process.env.JWT_SECRET;
if (!secret) throw new Error('JWT_SECRET is not set');

const token = jwt.sign({ sub: userId }, secret, { algorithm: 'HS256', expiresIn: '15m' });
const payload = jwt.verify(token, secret, { algorithms: ['HS256'] });

Use node --env-file=.env app.js on current Node.js versions, or the dotenv package. Fail on startup when the variable is missing rather than falling back to a default.

4. Separate secrets for access and refresh tokens

If you issue both access and refresh tokens, give each its own secret so a leak of one does not compromise the other:

JWT_ACCESS_SECRET=...
JWT_REFRESH_SECRET=...

5. Docker and production

  • Docker: pass it at runtime with --env-file or your orchestrator's secrets, not with ENV in the Dockerfile, which bakes it into the image.
  • Hosting platforms: set it in the project's environment variable or secrets settings. Use a different value per environment.
  • Cloud secret managers: AWS Secrets Manager, Google Secret Manager or HashiCorp Vault can inject the value at startup and make rotation easier.

6. If it leaks

Generate a new secret, deploy it, and accept that existing tokens signed with the old one become invalid and users must sign in again. Short token lifetimes limit the damage while you rotate.

Checklist

  • At least 32 random bytes for HS256.
  • .env is in .gitignore.
  • The app refuses to start without the variable.
  • Different secrets for each environment and for access vs refresh tokens.
  • You pin the algorithm when verifying tokens.

Need another kind of secret? Try the secret key generator or the JWT token generator.

Related Articles

Quick JWT Key Generation Guide

Learn how to quickly generate secure JWT keys using JWTSecrets.com. Complete guide with examples for Node.js, Next.js, and Python.

Read Article

JWT Security Best Practices 2025: Why JWTSecrets.com Sets the Gold Standard

Comprehensive guide to JWT security best practices in 2025. Learn why choosing the right tools and following proven security patterns is crucial for protecting your applications and user data.

Read Article