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
- Schema Validation: Define required environment variables and insecure default values.
- .env Enforcement: Compare your actual
.env
file against a provided.env.example
file. - Configurable Behavior: Choose whether to warn or throw errors, and control key matching.
- TypeScript Support: Fully written in TypeScript with complete type definitions.
- Cross-Platform: Works in Node.js and integrates seamlessly into your deployment workflows.
- Node.js v14 or higher
- npm v6 or higher
npm 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
: Whenfalse
, it warns if there are extra keys in your.env
or missing keys compared to.env.example
.throwOnError
: Whentrue
, 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
.env
file 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.example
file. You can safely ignore them.
Parameters:
-
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 tofalse
, EnvGuard will warn about extra keys or missing keys between.env
and.env.example
. -
throwOnError?: boolean
Iftrue
, 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:
- Run the demo script (with no
basedir
option):npm run demoNoBasedir
- Run the demo script (with
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
- Login to npm:
npm login
- Publish the package:
npm publish --access public
Contributions are welcome! Follow these steps:
- Fork the Repository
- Create a Feature Branch:
git checkout -b feature/my-new-feature
- Commit Your Changes
- Submit a Pull Request
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! 🛡️