Environment Variable Management: Secure Secrets and Deployment Practices
Keep environment configuration safe across local development, CI, and production without leaking API keys into browser bundles or logs.
TechSilo
Technology writer & developer
Environment variables separate configuration from source code, but they are not a magical secret store. Their security depends on who can read the process environment, deployment settings, logs, and generated bundles.
Separate public configuration from secrets
A browser may need a public URL or feature flag. It does not need private API credentials.
NEXT_PUBLIC_SITE_URL=https://www.example.com
DATABASE_URL=postgres://...
SERVICE_API_KEY=...In Next.js, NEXT_PUBLIC_* variables can be included in browser-accessible JavaScript when referenced by client code. Never place passwords, private tokens, or privileged API keys behind a public prefix.
Prefer calling protected third-party services from a server API route that enforces authentication, quotas, and input validation.
Keep a safe local example
Commit a .env.example with placeholder names only:
DATABASE_URL=replace-with-your-database-url
SERVICE_API_KEY=replace-with-your-api-keyKeep real .env.local, .env, and production secrets excluded from version control. Review your .gitignore, and use a secret-scanning tool in CI.
If a secret was ever committed or shown publicly, rotate it. Deleting it from the latest commit does not make an exposed credential safe again.
Read values on the server
function requiredEnv(name) {
const value = process.env[name];
if (!value) throw new Error('Missing required configuration: ' + name);
return value;
}
const serviceApiKey = requiredEnv('SERVICE_API_KEY');Perform validation during startup or at the point of use, depending on your deployment design. Avoid logging the value when explaining an error. Use scoped messages that name only the missing setting.
Development, staging, and production
Use different credentials for different environments. A staging app should not have write access to production data unless there is a strong, reviewed operational reason.
Keep an inventory of:
- Which service uses each secret.
- Who can read or change it.
- What permissions the credential grants.
- The expected rotation procedure.
- How the app behaves when the secret is missing.
On cPanel, verify that your Node.js process actually receives the intended environment settings. A successful npm run build does not prove that server-side API calls have all the runtime secrets they need.
Plan for rotation
A safe rotation often involves issuing a new credential, deploying both sides of the transition if supported, switching traffic, verifying use, and revoking the old key. The exact order depends on the provider.
Prefer short-lived credentials or workload identity over long-lived shared keys when your hosting supports them.
Avoid secret leakage in logs
Do not dump process.env or print full error objects that may embed connection strings, authorization headers, or tokens. Review downloaded server logs before sharing them.
Deployment checklist
Confirm environment scoping, secret permissions, version-control ignores, build-time exposure, log redaction, and restart procedures. Secrets should remain private even when the application's source and frontend bundles are publicly accessible.
For related authentication guidance, see [API Authentication Patterns](/blog/practical-security-guide-for-api-authentication-patterns-msfk81xw).
Want to put this into practice?
Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.
Related blog posts
npm Scripts Cheat Sheet: Commands, Arguments, and Automation
A developer reference for package.json scripts, npm run, pre/post hooks, environment variables, and safe task automation.
Read postPractical CORS Configuration Security Guide for APIs
Secure cross-origin browser requests with explicit origin allowlists, correct preflight handling, and credential rules.
Read postAPI Authentication Patterns: Sessions, Tokens, and Service Credentials
Compare session cookies, bearer tokens, OAuth, and API keys, with practical protections against token leakage and authorization mistakes.
Read postPractical XSS Protection in React: Escaping, Sanitization, and CSP
Prevent cross-site scripting in React by understanding text escaping, dangerous HTML injection, safe URL handling, and Content Security Policy.
Read postPreventing SQL Injection: Parameterized Queries and Safe Database Access
Learn how SQL injection works and how to prevent it using parameterized queries, least-privilege database accounts, and input validation.
Read post