Skip to content
Jennifer Programming Language

dotenv - .env configuration files

Import with import "dotenv.j" as dotenv;. Reads the KEY=VALUE files that keep configuration and secrets out of source, with layered environment profiles and ${VAR} interpolation. Over fs + strings + os + path + regex + maps; pure .j, runs on both binaries.

jennifer
import "dotenv.j" as dotenv;
use io;
use os;

# Single file ('...' keeps ${PORT} literal; "..." would interpolate it itself):
def cfg as map of string to string init dotenv.parse("PORT=8080\n" + 'URL="http://h:${PORT}"');
io.printf("%s\n", $cfg["URL"]);       # http://h:8080

# Layered, real-env-wins load (profile from JENNIFER_ENV):
dotenv.autoload(os.cwd());

Runnable: examples/modules/dotenv_demo.j.

Functions

Single-file primitives

CallReturnsNotes
dotenv.parse(text)map of string to stringParse .env text; touches nothing else.
dotenv.read(path)map of string to stringRead and parse one file.
dotenv.load(path)map of string to stringRead, parse, and os.setEnv each variable (unconditional override); returns the map. A key that is not a valid environment name ([A-Za-z_][A-Za-z0-9_]*) is a dotenv error.

load overwrites whatever is already in the environment. For a layered, real-env-wins load, use the cascade loaders below.

Layered loaders

Merge, later overriding earlier and skipping absent files, from one explicit base directory (no search, no walk-up):

text
.env  ->  .env.local  ->  .env.<profile>  ->  .env.<profile>.local

The <profile> comes from the profile argument (or JENNIFER_ENV for autoload); an empty profile loads only the two base files (there is no .env.default). At the command line, jennifer run --env=prod app.j sets JENNIFER_ENV=prod before the program runs (identical to JENNIFER_ENV=prod jennifer run app.j), which picks the profile autoload reads; see Run profiles.

CallReturnsNotes
dotenv.readCascade(dir, profile)map of string to stringThe merged file layers. No env mutation.
dotenv.resolve(dir, profile)map of string to stringThe effective map: readCascade with the real OS env overlaid (real env wins). No env mutation.
dotenv.loadCascade(dir, profile)map of string to stringreadCascade, then os.setEnv only keys not already set in the real env (an invalid name is an error, as in load). Returns the file map.
dotenv.autoload(dir)map of string to stringloadCascade(dir, JENNIFER_ENV): the profile is the JENNIFER_ENV variable.

Value precedence, highest first: a pre-existing OS env var (never overwritten)

.env.<profile>.local > .env.<profile> > .env.local > .env.

Syntax

Each non-blank, non-comment line is a KEY=VALUE assignment:

dotenv
# a comment line
PORT=8080
export NAME="ada lovelace"     # a leading `export` is ignored
GREETING='hi # not a comment'  # single quotes are literal
BANNER="line one
line two"                      # double quotes may span physical lines
URL="http://localhost:${PORT}" # ${VAR} interpolation
TOKEN=abc123        # trailing inline comment (unquoted values only)
EMPTY=
  • Comments: a line starting with # is skipped; on an unquoted value a # (space then hash) starts an inline comment. Inside quotes, # is literal.
  • export: a leading export prefix is stripped, so a file that doubles as a shell script parses the same.
  • Double quotes expand the escapes \n, \t, \r, \" and \\, and may span multiple physical lines; the real newlines become part of the value. Any other escape (\q, \$, a backslash ending a line) is an error.
  • Single quotes are fully literal (no escapes, no interpolation) and end on the same line.
  • After a closing quote only whitespace and a # comment may follow.
  • Unquoted values are trimmed of surrounding whitespace.
  • The value may contain = (only the first = splits the line); a later duplicate key wins.
  • Malformed input is an error, never skipped: a line with no =, an empty key, an unterminated quote, text after a closing quote or an unknown escape throws Error{kind: "dotenv"} whose message and line field name the line.

Interpolation

${VAR} in an unquoted or double-quoted value is replaced with VAR's value. Resolution is backward-reference only; it looks up, in order:

  1. a key already parsed (earlier in this file, or an earlier cascade layer),
  2. the real OS environment,
  3. the empty string.

Because a reference sees only keys defined before it, cycles are impossible (a forward reference resolves to "").

  • A single-quoted value is fully literal ('${X}' stays ${X}).
  • A ${...} whose contents are not a valid variable name is kept literal.
  • There is no $(...) / backtick command substitution: those are plain characters, so a .env value can never execute a command.

Security

.env* files are trusted local input: the developer controls the working directory; dotenv is not a sandbox. Within that model:

  • Strict profile validation. A profile label must match ^[A-Za-z0-9_-]{1,64}$ before it is spliced into .env.<profile>, so a JENNIFER_ENV from an untrusted upstream cannot path-traverse (../../etc/x, prod/../y). An invalid label is a dotenv error.
  • Fixed base directory, no search, no walk-up. There is no "nearest .env" walk an attacker could poison by planting a file in a parent directory. .env.local overriding .env is safe because both live in the same developer-controlled directory.
  • Real env wins. The cascade loaders (resolve / loadCascade / autoload) never let a committed file clobber a real environment variable, so a deployment's real secret beats a file value. (The single-file load still overrides unconditionally.) To force-override from a cascade, use readCascade + your own os.setEnv loop.

A variable set to the empty string in the real environment is set, so it wins: FOO= (as Docker and Compose pass an empty variable) keeps FOO empty rather than taking the file value. Only an unset variable takes the file value; os.unsetEnv clears one.

Scope

  • No $VAR (unbraced) interpolation, only ${VAR}. A bare $FOO is literal.
  • Multi-line applies to double quotes only; a single-quoted value is one line.

See also

  • fs.md - the file reads the loaders use.
  • os.md - os.setEnv / os.getEnv for the environment.
  • path.md - path.join builds the layer paths.
  • toml.md - for richer, typed, nested configuration.
  • modules/index.md - the module catalog and import rules.

API reference

Every exported function, struct and constant of dotenv, with its signature, generated from the module's doc comments: the dotenv API reference.