UNPKG

env-checker-strict

Version:

Strict environment variable checker with autogenerated ENV_LIST and CLI support

146 lines (107 loc) โ€ข 4.59 kB
# 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 ๐Ÿ™Œ