Loading repository data…
Loading repository data…
hoangsonww / repository
🛡️ EnvGuard – A powerful NPM package that validates your .env files against a predefined schema, ensuring all required environment variables are set and secure. Prevent misconfigurations in production by detecting missing values, insecure defaults, and enforcing .env.example consistency across teams.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
EnvGuard is an NPM package that validates your environment variables against a defined schema and enforces consistency with your .env.example file. Protect your application from misconfigurations and insecure defaults when working in teams or deploying to production.
Currently available on NPM: https://www.npmjs.com/package/@hoangsonw/env-guard
.env.env.examplenpm install @hoangsonw/env-guard
yarn add @hoangsonw/env-guard
EnvGuard validates your environment variables based on a schema. It loads variables from your .env file and compares them against a reference .env.example.
Create a schema for your environment variables and validate:
import { validateEnv } from "@hoangsonw/env-guard";
const schema = {
DB_HOST: { required: true, insecureValues: ["localhost", "127.0.0.1"] },
DB_PASSWORD: { required: true, insecureValues: ["12345", "password"] },
DB_USER: { required: false },
};
validateEnv({
schema,
envFilePath: "./.env", // Defaults to "./.env"
exampleFilePath: "./.env.example", // Defaults to "./.env.example"
allowMissingExampleKeys: false, // Warn if keys mismatch
throwOnError: false, // Only warn; set to true to throw errors
});
You can customize EnvGuard’s behavior by changing options:
allowMissingExampleKeys: When false, it warns if there are extra keys in your .env or missing keys compared to .env.example.throwOnError: When true, the function will throw errors instead of just logging warnings.Example:
import { validateEnv } from "@hoangsonw/env-guard";
const schema = {
API_KEY: { required: true },
DB_HOST: { required: true, insecureValues: ["localhost"] },
DB_PASSWORD: { required: true, insecureValues: ["password", "12345"] },
};
try {
validateEnv({
schema,
envFilePath: "./config/.env",
exampleFilePath: "./config/.env.example",
allowMissingExampleKeys: false,
throwOnError: true,
});
console.log("Environment variables validation passed!");
} catch (error) {
console.error("Environment validation failed:", error);
process.exit(1);
}
Note: The script might also parse environment variables from outside the
.envfile if they are already set in the environment of your machine or Node.js process. This can lead to some warnings in the console, if they are not defined in the.env.examplefile. You can safely ignore them.
validateEnv(options: EnvGuardOptions): voidParameters:
schema: EnvSchema
An object defining each environment variable’s requirements.
Example:
{
DB_HOST: { required: true, insecureValues: ["localhost"] },
API_KEY: { required: true }
}
envFilePath?: string
Path to your .env file. Defaults to "./.env".
exampleFilePath?: string
Path to your .env.example file. Defaults to "./.env.example".
allowMissingExampleKeys?: boolean
If set to false, EnvGuard will warn about extra keys or missing keys between .env and .env.example.
throwOnError?: boolean
If true, the function will throw an error when validations fail; otherwise, it will only log warnings.
Returns:
Nothing; it performs validation and logs warnings/errors as configured.
EnvGuard includes a Jest test suite. To run tests:
Install dependencies:
npm install
Run tests:
npm test
Test files in the __tests__ directory demonstrate how EnvGuard validates environment variables and compares .env to .env.example.
Run the demo scripts in the __tests__ directory to see EnvGuard in action:
basedir option):
npm run demoNoBasedir
basedir option):
npm run demoWithBasedir
The demo scripts will show how EnvGuard validates environment variables and compares .env to .env.example. Check the console output for validation results.
Compile the TypeScript source:
npm run build
npm login
npm publish --access public
Contributions are welcome! Follow these steps:
git checkout -b feature/my-new-feature
For major changes, please open an issue first to discuss your ideas. If you have any questions or need help, feel free to reach out.
This project is licensed under the MIT License.
EnvGuard ensures your environment variables are correctly configured and secure, reducing misconfigurations in team settings and production deployments. With schema validation and .env.example enforcement, it helps maintain consistency and security in your projects.
Happy guarding! 🛡️