8000 GitHub - onurartan/dotenvx: dotenvx is a modern, type-safe alternative to .env files — with validation, smart interpolation, enums, defaults, CLI support, and VSCode integration. Configuration and schema live together in a single .envx file.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dotenvx is a modern, type-safe alternative to .env files — with validation, smart interpolation, enums, defaults, CLI support, and VSCode integration. Configuration and schema live together in a single .envx file.

License

Notifications You must be signed in to change notification settings

onurartan/dotenvx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dotenvx

dotenvx logo

Dotenvx is a modern and type-safe alternative to traditional .env files. It brings validation, type inference, schema support, and a minimal CLI — all in a single developer-friendly solution.

Why dotenvx?

Traditional .env files lack structure and safety:

  • ❌ No type validation (everything is a string)
  • ❌ No way to define required or optional values
  • ❌ No default values or enums
  • ❌ No documentation inside .env itself

dotenvx solves this by allowing both configuration and schema to live in the same file (.envx), with first-class support for validation, multiline values, conditional logic, and more.


Features

  • ✅ Schema support with types, defaults, and requirements
  • ✅ Validation on load, with meaningful errors
  • ✅ Smart variable interpolation & ternary expressions
  • ✅ Triple-quoted multiline values
  • ✅ Built-in CLI (dotenvx) for automation and CI/CD use
  • ✅ Type-safe runtime access via getEnvx()
  • ✅ Compatible with process.env usage

Installation

pnpm add dotenvxjs
# or
npm install dotenvxjs

Programmatic Usage

import { loadEnvx, getEnvx } from "dotenvxjs";

// Loads and validates the .envx file (default is "./.envx")
loadEnvx();

const env = getEnvx();

console.log(env.PORT);        // typed access, number
console.log(env.API_URL);     // resolved with ternary + interpolation
console.log(env.NODE_ENV);    // enum value

Once loaded, all validated values are also available in process.env.


Example .envx File

DEV_MODE=false

API_URL=${DEV_MODE} ? "http://localhost:3000" : "https://api.example.com"
API_TOKEN=${DEV_MODE} ? "dev-token" : "prod-token"
FULL_API_URL="${API_URL}?token=${API_TOKEN}&env=${NODE_ENV}"

PORT=8080
DATABASE_NAME="my_db"

WELCOME_TEXT="""
Welcome to the new config system.
This supports multi-line values easily.
"""

# schema definition starts here
[DEV_MODE]
type="boolean"

[PORT]
type="number"
required=true

[API_URL]
type="string"

[NODE_ENV]
type="enum"
values=["production", "development", "test"]
default="development"
required=true

[DATABASE_NAME]
type="string"
required=true

[WEBSITE_URL]
type="url"
required=true
deprecated=true

CLI Usage

The dotenvx CLI is installed automatically with the package.

npx dotenvx build

Generates a .env file from your .envx:

npx dotenvx build

Options:

Option Description Default
-i, --input Input .envx file .envx
-o, --output Output .env file .env
--overwrite Overwrite if exists false

npx dotenvx check

Validates .envx against its declared schema:

npx dotenvx check

npx dotenvx print

Prints the parsed and interpolated .envx as JSON:

npx dotenvx print

npx dotenvx types

Generates a .ts file with full TypeScript typings based on the schema:

npx dotenvx types

Options:

Option Description Default
-o, --output Output .ts file envx.ts

Configuration File

You can create an optional envx.config.json in your project root to provide defaults for CLI options.

// envx.config.json (NEW)
{
  input: ".envx",
  outputs: {
    env: ".env",
    types: "src/envx.ts",
  },
  overwrite: true,
}

Type Definitions

If you want to use envx with TypeScript safely in your project:

// Load config
loadEnvx();

// Use with TS autocompletion
const env = getEnvx();

If you're using npx dotenvx types, it will auto-generate typings you can import:

import { Env } from "./envx.ts";

const env: Env = getEnvx();

Editor Integration

dotenvx is fully compatible with the official VSCode extension for .envx files, which provides:

  • Syntax highlighting
  • Auto-completion
  • Inline validation errors and warnings
  • Hover tooltips for schema metadata such as description and deprecated

Note: Fields like description and deprecated are only used by the VSCode extension for developer experience and do not affect runtime behavior or validation.


File Structure Suggestion

Roadmap

  • Ternary & interpolation support
  • CLI interface
  • Type-safe runtime API
  • Enum & default handling
  • VSCode syntax plugin
  • Web playground for .envx files
  • Linting rules and formatting

License

MIT © Trymagic

About

dotenvx is a modern, type-safe alternative to .env files — with validation, smart interpolation, enums, defaults, CLI support, and VSCode integration. Configuration and schema live together in a single .envx file.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0