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 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 recursively using:- $VAR[:default]
or ${VAR[:default]}
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. Multiline values are quoted. The returned object from getDotenv
matches the file contents.
If log
is set, the resulting map is logged. If loadProcess
is set, the map is merged into process.env
(string values only).