1.0.2 • Published 4 months ago

easyenvguard v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

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:

npm install easyenvguard

Or with yarn:

yarn add easyenvguard

šŸ”§ Usage

1ļøāƒ£ Create a .env file (if using dotenv)

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

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

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 TypeAccepted Values Example
stringAny text ("example")
numberNumeric values (3000, 42)
booleantrue or false (as strings)

āš ļø Error Handling

If an environment variable is missing or has the wrong type, EasyEnvGuard will throw an error:

EnvGuardError: Missing required environment variable: JWT_SECRET
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 VersionSupport
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