dotenv
Loads environment variables from a .env file into process.env for Node.js apps without changing existing values.
Official Documentation ↗v18.0.9. Check the official docs for the latest API surface.Installation
npm install dotenvpnpm add dotenvMethods
No methods match this filter.
Loads environment variables from a .env file into process.env for Node.js apps without changing existing values.
Overview
dotenv moves local configuration out of shell profiles and into a committed-or-ignored .env file. A single config call reads KEY=value lines and merges them into process.env without disturbing variables that already exist.
The parser supports quoted values with escapes, multiline values in quotes, and comments, and can be used standalone through parse for tooling that inspects env files. Multiple paths load in order for layered local overrides, while production configuration should come from the platform environment instead.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
config— Reads the .env file and populates process.env, skipping keys that already exist unless override is set.parse— Parses env-file content into a plain object without touching process.env, useful for inspection and tests.path / override options— Points config at one or more files, and lets file values replace pre-existing environment variables when override is true.
Usage Notes
- Commit .env.example with placeholders and gitignore the real .env so newcomers know which keys exist.
- Keep secrets out of repositories: dotenv is a loading convention, not a secret manager; rotate anything committed by accident.
- Pair dotenv with startup validation so a missing key fails fast with a clear message rather than a runtime undefined.
Common Pitfalls
- Values are always strings: DATABASE_PORT becomes “5432”, and comparisons against numbers silently fail.
- config must run before modules that capture process.env at import time, or they see undefined forever.
- Multiline values require quotes; unquoted line breaks terminate the value and orphan the next line.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/motdotla/dotenv@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:16.856Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Why am I getting the error Module not found: Error: Can’t resolve ‘os|path’? (https://cdn.jsdelivr.net/gh/motdotla/dotenv@latest/README.md)
Security notes
Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/motdotla/dotenv@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:16.856Z.
- Encrypt them with dotenvx encrypt -f .env and safely include them in source control. (https://cdn.jsdelivr.net/gh/motdotla/dotenv@latest/README.md)
- Related Tools * Expand environment variables with dotenv-expand * Cloak your secrets in VS Code * Encrypt your .env files with dotenvx ## CHANGELOG See CHANGELOG.md ## Who’s using dotenv? (https://cdn.jsdelivr.net/gh/motdotla/dotenv@latest/README.md)
FAQ
- Does dotenv override real environment variables?
- No. By default, values already present in process.env stay untouched, so deployment platforms and containers keep precedence. Pass override: true only when you deliberately want the file to win.
- Where should config() be called?
- As early as possible in the entry file, before modules that read process.env at import time. import "dotenv/config" is the safest form because it runs before any subsequent import.
- How do I validate that required variables exist?
- dotenv only loads values. Follow up with a schema check: parse the result or process.env into a validator such as zod or envalid and fail at startup with a precise list of missing keys.
Report a problem with this API docOpens a pre-filled issue on GitHub.