@ctroenv/node
v1.1.2
Published
Node.js adapter for CtroEnv — process.env source and .env file loading
Maintainers
Readme
@ctroenv/node
Node.js adapter for CtroEnv — load .env files and access process.env.
Installation
npm install @ctroenv/nodeUsage
import { defineEnv } from "@ctroenv/core"
import { loadEnv } from "@ctroenv/node"
const env = defineEnv(schema, {
source: loadEnv(),
})API
| Function | Description |
|----------|-------------|
| loadEnv(opts?) | Load .env files and return an EnvSource |
| nodeSource() | Create an EnvSource from process.env |
| parseEnvFile(content) | Parse a .env file string into key-value pairs |
loadEnv
loadEnv({ path?, encoding?, override?, system?, native? }): EnvSourceReads .env, .env.{NODE_ENV}, and .env.local files in priority order. Returns an EnvSource compatible with defineEnv().
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| path | string | process.cwd() | Directory to search for .env files |
| encoding | BufferEncoding | "utf-8" | File encoding |
| override | boolean | false | Prefer file values over process.env |
| system | boolean | false | Fall back to process.env for missing keys |
| native | boolean | false | Use Node.js built-in process.loadEnvFile() when available (Node 21.7+) |
nodeSource
nodeSource(): EnvSourceSimple process.env wrapper. Use when you don't need .env file loading.
parseEnvFile
parseEnvFile(content: string): Record<string, string>Parses a raw .env file string. Supports:
- Comments (
#) export KEY=valprefix- Quoted values (
"..."and'...') - Inline comments:
KEY=val # comment - Multiline values with trailing
\ - Variable interpolation:
KEY=$OTHERandKEY=${OTHER} - Escaped quotes:
KEY=it'sorKEY=val\"ue $$escaping for literal dollar signs
Documentation
Full documentation at ctroenv.vercel.app
License
MIT
