Environment Variable Validator
This script ensures that all required environment variables are present across all environment files (.env.dev, .env.staging, .env.prod), local + secrets (.env.local) and the global (.env).
Files Checked
./environments/.env.dev./environments/.env.staging./environments/.env.prod.env.localfor local development and secrets.envfor general variables
Whenever a new environment variable is added or removed in your app, this script must be updated to reflect that change.
Setup
Make sure to install dotenv package if you haven’t already:
npm install dotenvHow to use
Copy the script code in required-env.js in the root of your project
const dotenv = require("dotenv");
const fs = require("fs");
// Change this line depending on the environment you want to check
const environmentSpecificPaths = [
"./environments/.env.dev",
"./environments/.env.staging",
"./environments/.env.prod",
];
const environmentSpecificRequiredEnvVars = [
// APP environment
// Default: "dev"
"APP_ENV",
];
const generalRequiredEnvVars = [
// For API calls
// Default: "phpreaction.com"
"NEXT_PUBLIC_API_URL",
];
const secretRequiredEnvVars = [
// APP secrets
// Should be generated
"APP_SECRET",
// CSRF and JWT secrets
// Should be generated
"SECURITY_JWT_SECRET",
];
// === HELPERS ===
function parseEnvFile(filePath) {
if (!fs.existsSync(filePath)) {
console.warn(`⚠️ ${filePath} file not found.`);
return null;
}
const content = fs.readFileSync(filePath, "utf-8");
const parsed = dotenv.parse(content);
return parsed;
}
function checkEnvVars(envVars, requiredKeys, sourceName) {
const missing = [];
requiredKeys.forEach((key) => {
if (envVars[key]) {
console.log(`✅ ${key} : ${envVars[key]}`);
} else {
console.warn(`❌ ${key}`);
missing.push(key);
}
});
if (missing.length > 0) {
console.error(
`\n⚠️ Missing variables in ${sourceName}: ${missing.join(", ")}`
);
}
}
// === MAIN FUNCTION ===
console.log("\n\nApp - Environment Variables Checker");
console.log("========================================");
// General vars check from process.env
console.log("\n🔍 Checking general environment variables (.env)");
checkEnvVars(process.env, generalRequiredEnvVars, "global process.env");
// Secrets vars check from process.env
console.log("\n\n🔍 Checking secrets environment variables (.env.local)");
console.warn(
"🔐 Ensure that secrets are not commited to repositories! They should be only in .env.local (The root one, not .env.environments/.env.local)"
);
const parsedLocalEnv = parseEnvFile(".env.local");
if (parsedLocalEnv) {
checkEnvVars(parsedLocalEnv, secretRequiredEnvVars, ".env.local");
} else {
console.warn("⚠️ Skipping .env.local due to parsing error.");
}
environmentSpecificPaths.forEach((path) => {
console.log(`\n\n🔍 Checking specific environment (${path})`);
const parsedEnv = parseEnvFile(path);
if (parsedEnv) {
checkEnvVars(parsedEnv, environmentSpecificRequiredEnvVars, path);
} else {
console.warn(`⚠️ Skipping ${path} due to parsing error.`);
}
});
// Check env in bundles
const bundles = [
"@phpcreation/frontend-auth-authorization-flow-react-nextjs-bundle",
"@phpcreation/frontend-components-react-nextjs-bundle",
"@phpcreation/frontend-config-react-nextjs-bundle",
"@phpcreation/frontend-crud-react-nextjs-bundle",
"@phpcreation/frontend-dynamodb-react-nextjs-bundle",
"@phpcreation/frontend-icons-react-nextjs-bundle",
"@phpcreation/frontend-status-react-nextjs-bundle.git",
"@phpcreation/frontend-utils-react-nextjs-bundle",
];
console.log("---------------------------------");
console.log("\n\n🔍 Checking environment variables in bundles");
bundles.forEach((bundle) => {
// Run the bundle's required-env.js script
const bundlePath = `./node_modules/${bundle}/required-env.js`;
if (fs.existsSync(bundlePath)) {
try {
if (bundlePath.includes("icons")) {
import(
"./node_modules/@phpcreation/frontend-icons-react-nextjs-bundle/required-env.js"
);
} else {
require(bundlePath);
}
} catch (error) {
console.error(`❌ Error running ${bundlePath}:`, error.message);
}
} else {
console.warn(`⚠️ ${bundlePath} not found.`);
}
console.log("---------------------------------");
});Fill environmentSpecificRequiredEnvVars
Fill the environmentSpecificRequiredEnvVars with the required environment variables for .env.dev, .env.staging, .env.prod
Fill generalRequiredEnvVars
Fill the generalRequiredEnvVars with the required environment variables for the global .env file.
Fill secretRequiredEnvVars
Fill the secretRequiredEnvVars with the required environment variables that should be secrets and not committed to the repository (.env.local)
Run the script
node -r dotenv/config required-env.jsNote: You can take inspiration from project like inventory to see how to manage environment variables and validation.