env-checker-strict
v1.0.4
Published
Strict environment variable checker with autogenerated ENV_LIST and CLI support
Maintainers
Readme
env-checker-strict
Ever crashed your server on a weekend due to a missing environment variable? env-checker-strict is a strict and developer-friendly environment variable checker that ensures all required env vars are validated before your app runs. Add the env reference to version control, and catch issues in production — not during your downtime.
🚀 Why use this?
Managing environment variables in large codebases can be error-prone:
- Missing env vars crash the app only during runtime ⚠️
- You don't get autocomplete or type checking 🧠
- Keeping track of all required keys is messy 📝
env-checker-strictsolves these with a CLI that auto-generates acheckEnvAndThrowError()function and a typedENVSaccessor.
✨ Features
- ✅ Throws on missing env vars at startup
- 🧠 Generates
ENVSobject with keys for autocomplete and type safety - ⚙️ Supports both JavaScript and TypeScript
- 🧪 Fully customizable CLI: skip keys, comment block, output file path
- 📄 Auto-detects and parses
.envfile - 🔌 Auto injects required import and function call to entry file (optional with
--injectflag) see CLI Options
📦 Installation
npm install -D env-checker-strictFor global CLI usage:
npm install -g env-checker-strict🧑💻 CLI Usage
env-checker-strict --input .env --output env_checker.js --skip "SECRET,API_KEY" --tsCLI Options
| Option | Alias | Description | Default |
|---------------|-------|-----------------------------------------------------------------------------------------------|--------------------|
| --input | -i | Path to .env file | .env |
| --output | -o | Output file path for the generated checker | env_checker.js |
| --skip | -s | Comma-separated list of env keys to skip from validation | (none) |
| --comment | -c | Add a reminder comment at the top of .env to run env-checker-strict | true |
| --ts | | Generate TypeScript output instead of JavaScript | false |
| --inject | | Inject checkEnvAndThrowError() into the entry file. Optionally pass a path to the entry file | Read from package.json > main |
🔧 Example
Given .env
JWT_SECRET=supersecure
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
API_KEY=CLI Run
env-checker-strict -i .env -o env_checker.ts --ts -s API_KEYOutput: env_checker.ts
// Auto-generated by env-checker-strict
// ⚠️ Do not modify manually
export const ENV_LIST: string[] = [
'JWT_SECRET',
'DB_HOST',
'DB_USER',
'DB_PASSWORD'
];
export interface ENVS {
JWT_SECRET: string;
DB_HOST: string;
DB_USER: string;
DB_PASSWORD: string;
}
export const ENVS: ENVS = {
JWT_SECRET: process.env.JWT_SECRET as string,
DB_HOST: process.env.DB_HOST as string,
DB_USER: process.env.DB_USER as string,
DB_PASSWORD: process.env.DB_PASSWORD as string
};
export const checkEnvAndThrowError = (): void => {
for (const env of ENV_LIST) {
if (!process.env[env]) {
const message =
`[ENV ERROR] Missing required environment variable: ${env}\n` +
`➡️ Make sure '${env}' is defined in your .env file.\n` +
`📄 Location: ${import.meta.url || 'env-checker.ts'}`;
console.error(message);
throw new Error(message);
}
}
};✅ How to Use in Project
JavaScript (index.js)
const { checkEnvAndThrowError, ENVS } = require('./env_checker');
checkEnvAndThrowError();
console.log(ENVS.JWT_SECRET); // Autocomplete for all env varsTypeScript (main.ts)
import { checkEnvAndThrowError, ENVS } from './env_checker';
checkEnvAndThrowError();
console.log(ENVS.JWT_SECRET); // Fully typed access💡 Tips
- Always run the CLI after updating your
.envfile. - Keep the generated file in source control (optional, but helpful).
- Use the
ENVSobject across your app instead ofprocess.env.
🪪 License
MIT — safe, free, and open.
Made with ❤️ to make your environment safer and your dev experience sharper.
PRs and suggestions welcome 🙌
