Browse packages

Package · v0.32.0 · changelog

env

Standard library environment variables

Runtime support

  • Node
  • Deno
  • Bun
  • Browser

Browser: Uses an empty environment. has() returns false, and get() requires a fallback.

This module provides required and optional access to environment variables.

Installation#

bash
npm install @rcompat/env
bash
pnpm add @rcompat/env
bash
yarn add @rcompat/env
bash
bun add @rcompat/env

Usage#

Required variables#

Use env.get when a variable must exist. Throws immediately if missing, so misconfigured environments fail early and explicitly:

js
import env from "@rcompat/env";

const url = env.get("DATABASE_URL");   // string, throws if missing
const secret = env.get("API_SECRET");  // string, throws if missing

Fallbacks and presence#

Pass a fallback to env.get(key, fallback) to use it when the variable is missing. Stored values, including empty strings, take precedence. An omitted or explicitly undefined fallback keeps the missing-variable error. Use env.has(key) to check whether a variable exists.

js
import env from "@rcompat/env";

const port = env.get("PORT", "6161");
const debugConfigured = env.has("DEBUG");

Browsers#

Browser builds expose an empty environment. env.has(key) returns false. env.get(key, fallback) returns a defined fallback and throws if the fallback is omitted or undefined.

File loading#

The package looks for the first matching file in your project root:

  1. .env.{NODE_ENV}.local
  2. .env.{NODE_ENV}
  3. .env.local
  4. .env

If NODE_ENV is not set, JS_ENV is used as a fallback. Local files (.local suffix) are typically gitignored and used for machine-specific secrets.

Merging#

The runtime's native environment is always the base. Node and Bun read from process.env, while Deno reads from Deno.env. The first .env file found is layered on top, so file variables win over system variables.

Substitution#

Variables can reference other variables:

FOO=world
BAR=hello$FOO           # helloworld
BAZ=hello${FOO}         # helloworld
ESCAPED=hello\$FOO      # hello$FOO
WITH_DEFAULT=${FOO:-fallback}  # uses fallback if FOO is undefined

Substitution resolves against the merged environment, so system variables are available too:

CONFIG=$HOME/.config/myapp
DB_USERNAME=${DB_USERNAME:-myapp}  # uses system DB_USERNAME or falls back to myapp

Deno permissions#

Deno requires --allow-env to read the system environment and --allow-read to discover and load dotenv files. Permission failures are reported by Deno during module initialization rather than being treated as missing variables.

Single-quoted values skip substitution:

FOO='hello$BAR'     # hello$BAR (literal)

Project structure#

my-project/
├── .env                    # Shared defaults
├── .env.development        # Development settings
├── .env.development.local  # Local dev secrets (gitignored)
├── .env.production         # Production settings
└── .gitignore
gitignore
.env.local
.env.*.local

License#

MIT

Contributing#

See CONTRIBUTING.md in the repository root.