UNPKG

easyenvguard

Version:

Developers often forget to define required environment variables (.env files). This package will automatically validates required environment variables on startup.

135 lines (94 loc) • 3.79 kB
# EasyEnvGuard - Environment Variable Validator ## šŸ“Œ Introduction **EasyEnvGuard** is a lightweight and easy-to-use environment variable validator for Node.js applications. It ensures that required environment variables are defined and have the correct data types, preventing runtime errors due to missing or incorrectly formatted variables. Built with simplicity and reliability in mind, **EasyEnvGuard** is an essential tool for developers looking to enhance the robustness of their applications. ## šŸš€ Features - āœ… Ensures required environment variables are present. - āœ… Validates data types (`string`, `number`, `boolean`). - āœ… Supports both **CommonJS** (`require`) and **ES Modules** (`import`). - āœ… Improves application reliability and maintainability. - āœ… Lightweight, fast, and dependency-free. - āœ… Easy integration with `.env` files. ## šŸ“¦ Installation Install **EasyEnvGuard** using npm: ```sh npm install easyenvguard ``` Or with yarn: ```sh yarn add easyenvguard ``` ## šŸ”§ Usage ### 1ļøāƒ£ Create a `.env` file (if using `dotenv`) ```env PORT=3000 DATABASE_URL=mongodb://localhost:27017 JWT_SECRET=mysecret DEBUG_MODE=true ``` ### 2ļøāƒ£ Use `easyenvguard` in your Node.js project #### āœ… **For CommonJS (`require`) Users** ```javascript require("dotenv").config(); // Load .env file const { validateEnv } = require("easyenvguard"); validateEnv({ PORT: "number", DATABASE_URL: "string", JWT_SECRET: "string", DEBUG_MODE: "boolean", }); console.log("āœ… All required environment variables are valid!"); ``` #### āœ… **For ES Module (`import`) Users** ```javascript import "dotenv/config"; // Load .env file import { validateEnv } from "easyenvguard"; validateEnv({ PORT: "number", DATABASE_URL: "string", JWT_SECRET: "string", DEBUG_MODE: "boolean", }); console.log("āœ… All required environment variables are valid!"); ``` ## āœ… Validation Rules | Expected Type | Accepted Values Example | | ------------- | ------------------------------ | | `string` | Any text (`"example"`) | | `number` | Numeric values (`3000`, `42`) | | `boolean` | `true` or `false` (as strings) | ## āš ļø Error Handling If an environment variable is missing or has the wrong type, `EasyEnvGuard` will throw an error: ```sh EnvGuardError: Missing required environment variable: JWT_SECRET ``` ```sh EnvGuardError: Invalid type for PORT. Expected number, but got string. ``` ## šŸ“– Best Practices - **Use `.env.example` files**: Maintain a template of required environment variables for better collaboration. - **Validate environment variables at application startup** to catch issues early. - **Use `dotenv` or environment managers** to manage environment variables across different stages (development, staging, production). ## šŸ› ļø Compatibility & Support | Node.js Version | Support | |----------------|---------| | 18.x+ | āœ… Fully Supported | | 16.x | āœ… Fully Supported | | 14.x | āš ļø Limited Support (End-of-Life soon) | | < 14.x | āŒ Not Supported | ## šŸ“œ License MIT License. Feel free to use and contribute! ## šŸ¤ Contributing Contributions are welcome! If you have ideas for improvements, submit an issue or a PR. ### How to Contribute 1. **Fork** the repository. 2. **Create** a new branch (`feature-branch`). 3. **Make** your changes and commit (`git commit -m "Added a new feature"`). 4. **Push** to your fork and submit a **pull request**. ## šŸ› ļø Roadmap - [ ] Add TypeScript support - [ ] Provide built-in support for custom validation functions - [ ] Improve error logging and debugging experience --- ⭐ **Star this repo if you find it useful!** šŸš€ šŸ”— [GitHub Repository](https://github.com/The-Somkene/easyEnvGuard)