get-dotenv loads variables from a deterministic cascade of dotenv files, per input path, then merges across paths.
Given:
.env (configurable)local (configurable)<ENV> (optional)Per input path, the loader evaluates up to four files in this order:
<token> (e.g., .env)<token>.<ENV> (e.g., .env.dev)<token>.<privateToken> (e.g., .env.local)<token>.<ENV>.<privateToken> (e.g., .env.dev.local)If a file is missing, it is silently skipped. Parsed values from later files override earlier ones (e.g., private values override public values).
When env is not set via -e, get-dotenv performs a global-only pre-pass — loading only global dotenv files (excluding env-scoped files) — and checks for a variable named by defaultEnvKey (default: DEFAULT_ENV).
Resolution order: explicit -e > defaultEnvKey value from global files > static defaultEnv from config.
The resolved environment then determines which env-scoped files (e.g., .env.dev, .env.dev.local) are loaded in the full cascade.
When paths contains more than one directory, get-dotenv visits each directory in the order they appear in the array. For each directory, the same four-file cascade is applied and merged into the overall result. Later paths override earlier paths for colliding keys.
Example:
{
"paths": ["./", "./packages/app"]
}
Values from ./packages/app win over ./ if the same key appears in both.
If dynamicPath is provided and excludeDynamic is not set, the module at that path is loaded and its default export is applied after parsing and expansion:
Keys are processed in object key order and can override previously loaded variables.
See the README “Dynamic Processing” section for details.
After merging, values are expanded once, using $VAR[:default] or ${VAR[:default]}:
undefined.\$ is a literal $, and a $ that starts no reference (cost $, $!) stays literal while the other references in the value still expand.http://$HOST:8080 reads 8080 as HOST's default: write http://${HOST}:8080. There is no :- form (${A:-x} has the default -x).vars/envVars, -v or process.env: with P=\$5, Q=$P and R=${P}x give Q=$5 and R=$5x. A default is expanded by the same rules (${A:${B}}). Each substituted value is spliced in as-is: it is never re-scanned with the surrounding text, and $&/$$ in it have no special meaning.process.env or an earlier layer); if there is none, it counts as unset and takes its default, else the empty string. So P=${P:/usr/bin}:/x appends to an ambient P, and gives /usr/bin:/x when nothing earlier sets P.See the API docs for dotenvExpand and dotenvExpandAll for precise behavior.
If outputPath is set, the fully expanded variables are written to a single dotenv file at the resolved path. Each value is quoted and escaped as needed (the dotenv editor's formatter; see Quoting) so that dotenv reads the file back to exactly the returned object; a value no quote style can hold throws, naming the key.
If log is set, the resulting map is logged. If loadProcess is set (it is off by default for getDotenv(), on by default for the CLI), the map is copied into process.env: a string sets the key, and an unset (undefined) value or a key deleted by a dynamic null deletes the key from process.env. The CLI host applies the same rule.