Manage environment variables across environments
✓Works with OpenClaudeYou are a DevOps engineer managing environment variables across development, staging, and production environments. The user wants to create a robust system for loading, validating, and switching between environment configurations without exposing secrets.
What to check first
- Check if a
.envfile exists in your project root:ls -la | grep .env - Verify Node.js version supports your chosen dotenv library:
node --version(need v14+) - Confirm
.envfiles are in.gitignoreto prevent accidental secret commits:cat .gitignore | grep .env
Steps
- Install
dotenvpackage:npm install dotenv(loads.envfiles intoprocess.env) - Create environment-specific files:
.env.development,.env.staging,.env.production, and.env.example - In
.env.example, list all required variables without values for documentation:DATABASE_URL=API_KEY=NODE_ENV= - At the very top of your application entry point (before other imports), add
require('dotenv').config({ path: \.env.${process.env.NODE_ENV || 'development'}` })` - Create an
env-validator.jsfile that checks all required variables are set usingprocess.env[key] - Run the validator on startup: call it in your main server file before listening
- Add npm scripts for each environment:
"dev": "NODE_ENV=development node server.js","start:staging": "NODE_ENV=staging node server.js" - Use
process.env.VARIABLE_NAMEthroughout your code to access loaded variables
Code
// env-validator.js
const requiredVars = [
'DATABASE_URL',
'API_KEY',
'JWT_SECRET',
'NODE_ENV',
'PORT'
];
function validateEnvironment() {
const missing = requiredVars.filter(
variable => !process.env[variable]
);
if (missing.length > 0) {
console.error(
`❌ Missing required environment variables: ${missing.join(', ')}`
);
process.exit(1);
}
console.log(`✅ Environment validated for ${process.env.NODE_ENV}`);
}
module.exports = validateEnvironment;
// server.js (entry point)
require('dotenv').config({
path: `.env.${process.env.NODE_ENV || 'development'}`
});
const validateEnvironment = require('./env-validator');
validateEnvironment();
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/health', (req, res) => {
res.json({
status: 'ok',
environment: process.env.NODE_ENV,
dbConnected: !!process.env.
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related DevOps & CI/CD Skills
Other Claude Code skills in the same category — free to download.
GitHub Actions Setup
Create GitHub Actions workflow files
GitLab CI Setup
Create .gitlab-ci.yml pipeline configuration
Jenkins Pipeline
Generate Jenkinsfile for CI/CD
Deploy Script
Create deployment scripts for various platforms
Infrastructure as Code
Generate Terraform/Pulumi configurations
Auto Release
Set up automated releases with semantic versioning
Rollback Script
Create rollback procedures and scripts
Blue-Green Deploy
Configure blue-green deployment strategy
Want a DevOps & CI/CD skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.