DOCUMENTATION
ENV VARIABLES
Introduction

Introduction to loadEnv

The loadEnv utility in VkrunJS provides an advanced way to load, parse, and validate environment variables from .env files. Unlike traditional .env loaders, loadEnv offers schema validation, automatic type inference, variable interpolation, and multi-file support, making environment configuration more reliable and maintainable.

Key Features

FeatureDescription
🔄 Auto ParsingAutomatically parses booleans, numbers, arrays, and objects when assigned to variables.
Schema ValidationEnsures correctness with strict typing.
🔍 Debug ModeLogs details for troubleshooting.
🔄 Multi-File SupportSupports merging multiple .env files.
🛠 Configurable OverrideDecide whether to overwrite existing process.env values.
🔀 InterpolationExpands references within .env files.
🌍 Environment-Based LoadingLoads .env.NODE_ENV if NODE_ENV is set (e.g., .env.test, .env.production). Defaults to .env otherwise.
🔒 Schema Validation (Optional)Enforce type safety by defining a schema for environment variables, avoiding misconfigurations.
🔍 Type InferenceAutomatically parses booleans, numbers, arrays, and objects when variables are retrieved via loadEnv().
🔢 Manual Typing or Auto-InferenceSupports explicit typing using <T> or automatic inference with InferOut<typeof schema>.
🔗 Variable InterpolationExpands variables referencing other variables (e.g., API_URL="$BASE_URL/api").
📂 Multiple File SupportAllows loading from multiple .env files, merging their values.
🔄 Configurable Override BehaviorControls whether existing process.env variables are overridden.
🛠 Graceful Error HandlingIgnores invalid lines, missing files, and malformed JSON, providing safe defaults.

Quick Start

1️⃣ Creating a .env File

Create a .env file in your project root:

API_KEY="your-api-key"
DEBUG=true
PORT=3000

2️⃣ Using loadEnv()

import { loadEnv } from "vkrun";
 
const envs = loadEnv();
 
console.log(envs.API_KEY); // "your-api-key"
console.log(envs.DEBUG); // true
console.log(envs.PORT); // 3000

Note: loadEnv() automatically parses booleans and numbers when assigned to a variable.


Copyright © 2024 - 2025 MIT by Mario Elvio