env-checker-strict
Version:
Strict environment variable checker with autogenerated ENV_LIST and CLI support
146 lines (107 loc) โข 4.59 kB
Markdown
# 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-strict` solves these with a CLI that auto-generates a `checkEnvAndThrowError()` function and a typed `ENVS` accessor.
## โจ Features
- โ
Throws on missing env vars **at startup**
- ๐ง Generates `ENVS` object 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 `.env` file
- ๐ Auto injects required import and function call to entry file (optional with `--inject` flag) [see CLI Options](#cli-options)
## ๐ฆ Installation
```bash
npm install -D env-checker-strict
```
For global CLI usage:
```bash
npm install -g env-checker-strict
```
## ๐งโ๐ป CLI Usage
```bash
env-checker-strict --input .env --output env_checker.js --skip "SECRET,API_KEY" --ts
```
### CLI 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`
```env
JWT_SECRET=supersecure
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
API_KEY=
```
### CLI Run
```bash
env-checker-strict -i .env -o env_checker.ts --ts -s API_KEY
```
### Output: `env_checker.ts`
```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)
```js
const { checkEnvAndThrowError, ENVS } = require('./env_checker');
checkEnvAndThrowError();
console.log(ENVS.JWT_SECRET); // Autocomplete for all env vars
```
### TypeScript (main.ts)
```ts
import { checkEnvAndThrowError, ENVS } from './env_checker';
checkEnvAndThrowError();
console.log(ENVS.JWT_SECRET); // Fully typed access
```
## ๐ก Tips
- Always run the CLI after updating your `.env` file.
- Keep the generated file in source control (optional, but helpful).
- Use the `ENVS` object across your app instead of `process.env`.
## ๐ชช License
MIT โ safe, free, and open.
---
Made with โค๏ธ to make your environment safer and your dev experience sharper.
---
PRs and suggestions welcome ๐