de-env
v1.1.6
Published
A command-line tool for managing environment variables
Maintainers
Readme
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:
- Manual Schema Definition: Define your environment variable types and requirements explicitly
- Automatic Schema Generation: Use
de-env <env-path> <output-path>command to automatically generate a schema from your.envfile
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=trueYou 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 blockNow 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: stringInstallation
npm install de-envUsage
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:
- Using
#!prefix in your.envfile:
#!
optional_VAR=value- 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
- Set up your environment variables (use
#!prefix for optional variables) - Run
de-env config.tsto automatically generate the schema or manually write schema - Use the
Envfunction in your code with full type safety
License
MIT
