Skip to content

v1noid/de-env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

de-env

npm GitHub stars

de-env

A simple and efficient environment variable parser that generates TypeScript/JavaScript files from your environment variables using a schema-based approach.

Visit our official website: de-env.v1noid.com

What it does

de-env takes your environment variables and converts them into a strongly-typed TypeScript/JavaScript module using a schema definition. It helps your project validate environment variables in two ways:

  1. Manual Schema Definition: Define your environment variable types and requirements explicitly
  2. Automatic Schema Generation: Use de-env <env-path> <output-path> command to automatically generate a schema from your .env file

Key Features

  • Generates a schema from your environment variables
  • Handles type casting (string, number, boolean)
  • Supports optional fields validation using #! prefix in .env
  • Supports different module formats (ESM/CommonJS)
  • Automatic TypeScript type generation for better debugging

Example

Given environment variables:

# Database configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb
#!
API_KEY="your-secret-key"  # Using #! marks this as optional
DEBUG=true

You can also use optional block

# starting of optional block with #!!!
#!!!
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb
API_KEY="your-secret-key"
DEBUG=true
#---
# use #--- to end the block

Now all the variable in between the comment are marked as optional

import { EnvSchema } from "de-env";

export const Env = EnvSchema({
  DB_HOST: ["string", "optional"],
  DB_PORT: ["number", "optional"],
  DB_NAME: ["string", "optional"],
  API_KEY: ["string", "optional"],
  DEBUG: ["string", "optional"]
});

Automatic Schema Generation

Running de-env .env config.env.ts will automatically generate:

// config.env.ts
import { EnvSchema } from "de-env";

export const Env = EnvSchema({
  DB_HOST: "string",
  DB_PORT: "number",
  DB_NAME: "string",
  API_KEY: ["string", "optional"], // Automatically marked as optional due to #!
  DEBUG: "boolean"
});

Manual Schema Definition

You can also manually define your schema:

// config.env.ts
import { EnvSchema } from "de-env";

export const Env = EnvSchema({
  DB_HOST: "string",
  DB_PORT: "number",
  DB_NAME: ["string", "optional"], // Mark as optional
  API_KEY: ["string", "optional"], // Mark as optional
  DEBUG: "boolean"
});

Now you can use your environment variables with full TypeScript support:

import Env from './config';

// TypeScript will provide autocomplete and type checking
console.log(Env("DB_HOST" /* Because of typescript
you will get the keys suggestion here */));    // Type: string
console.log(Env("DB_PORT"));    // Type: number
console.log(Env("API_KEY"));    // Type: string
console.log(Env("DEBUG"));      // Type: boolean

// Will throw an error if DB_NAME is not set in environment variables
console.log(Env("DB_NAME"));    // Type: string

Installation

npm install de-env

Usage

Automatic Schema Generation

Using cli tool to generate

de-env <env-path> <output-path>

Schema Types

The schema supports the following types:

  • "string" - String values
  • "number" - Numeric values (automatically converted)
  • "boolean" - Boolean values (automatically converted)

You can mark fields as optional in two ways:

  1. Using #! prefix in your .env file:
#!
optional_VAR=value
  1. Using an array with "optional" in your schema:
{
  optional_FIELD: ["string", "optional"],
  OPTIONAL_FIELD: "string"
}

Features

  • Automatic schema generation from environment variables using de-env
  • Manual schema definition for fine-grained control
  • Automatic type casting based on schema
  • optional field validation (using #! or schema definition)
  • TypeScript support with full type inference
  • Automatic type generation for better debugging

Workflow

  1. Set up your environment variables (use #! prefix for optional variables)
  2. Run de-env config.ts to automatically generate the schema or manually write schema
  3. Use the Env function in your code with full type safety

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors