easyenvguard
Version:
Developers often forget to define required environment variables (.env files). This package will automatically validates required environment variables on startup (Now supports TS).
135 lines (94 loc) ⢠3.79 kB
Markdown
# 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)