GitHub GitHub
docs / Env Variables

Environment Variables

LiteNode has built-in support for .env files — no external dependencies needed.

loadEnv()

Signature:

loadEnv(path?: string, options?: object): object

Loads variables from a .env file into process.env and returns them as an object.

Parameter Type Default Description
path string ".env" Path to the env file
options.override boolean false Override existing process.env values
options.silent boolean false Suppress errors if the file doesn't exist
// Load default .env
app.loadEnv()

// Load a specific file
app.loadEnv(".env.production")

// Override existing vars, suppress missing file error
app.loadEnv(".env.local", { override: true, silent: true })

Example .env File

# Server
PORT=8080
NODE_ENV=development

# Database
DB_HOST=localhost
DB_PORT=5432
DB_USER=admin
DB_PASS="my secure password!"

# Feature flags
DEBUG=true
ENABLE_CACHE=false

getEnv()

Signature:

getEnv(key: string, defaultValue?: any): any

Gets an environment variable with automatic type conversion. Returns defaultValue if the variable is not set.

Parameter Type Description
key string Variable name
defaultValue any Fallback if not set

Type conversions:

Raw value Converted to
"true" / "false" boolean
Numeric strings number
"null" null
"undefined" undefined
Anything else string
const port = app.getEnv("PORT", 5000) // number: 8080
const debug = app.getEnv("DEBUG", false) // boolean: true
const apiUrl = app.getEnv("API_URL", "https://api.example.com") // string

Accessing via process.env

After loadEnv(), variables are also available on process.env as strings (no type conversion):

app.loadEnv()
console.log(process.env.PORT) // "8080" (string)
console.log(process.env.NODE_ENV) // "development"

Multiple Environments

Create separate files for each environment and layer them:

// Load base vars
app.loadEnv()

// Layer environment-specific overrides
const env = process.env.NODE_ENV || "development"
app.loadEnv(`.env.${env}`, { override: true, silent: true })

Typical file layout:

  • .env — shared defaults
  • .env.development — local overrides
  • .env.test — test configuration
  • .env.production — production values

Best Practices

  • Add .env to .gitignore — never commit secrets
  • Store API keys, passwords, and tokens only in .env files, not in source code
  • Document every variable your app uses (even with a comment in a .env.example file)
  • For complex multi-environment setups in large apps, a dedicated library like dotenv remains an option

See the Examples page for a full configuration object pattern.