Node.js Reference ยท process
Node.js process.loadEnvFile() method
Reference for process.loadEnvFile() and util.parseEnv() in Node.js: load KEY=value settings from a .env file into process.env, with the rules for quotes and comments.
process.loadEnvFile(path) reads a file of KEY=value lines and copies the entries into process.env. It is built into Node.js, so small projects no longer need a package such as dotenv just to read a .env file. Without an argument it loads ./.env from the current working directory.
Variables that already exist in the environment are not overwritten: a real environment variable set by the shell, a container or a process manager always wins over the file. The companion function util.parseEnv(text) parses the same format from a string and returns an object without touching process.env.
process.loadEnvFile('./.env'); console.log(process.env.PORT);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
process.loadEnvFile(path?: string | URL | Buffer): void util.parseEnv(content: string): Record<string, string>
| Name | Description |
|---|---|
path |
Location of the file. Defaults to .env in the current working directory. |
content |
The text of an env file for util.parseEnv(). |
Returns: nothing from loadEnvFile(); a plain object of strings from parseEnv().
Errors: loadEnvFile() throws an error with code ENOENT when the file does not exist, so wrap the call if the file is optional. A file that cannot be read throws the usual file-system error.
Usage notes
- File format. One
KEY=valueper line. Lines starting with#are comments, and#after a value starts a comment too. An optionalexportprefix is accepted. Values may be wrapped in single quotes, double quotes or backticks. - Quotes and newlines. In double quotes
\nbecomes a newline; single quotes keep the text literally. Quoted values can span several lines. - No variable expansion.
A=$Bis the literal text, not the value ofB. - Everything is a string.
PORT=3000gives'3000'. Convert withNumber()and validate before use. - Existing variables win, which lets production settings override a developer's file without changing it.
- Command-line form.
node --env-file=.env app.jsloads the file before any code runs, so it also affects module-level code and Node options inNODE_OPTIONS.--env-file-if-existsignores a missing file. UseloadEnvFile()when you want to decide at runtime. - Do not commit secrets. Add
.envto.gitignoreand commit an example file with fake values. - Call it first, before reading configuration at import time elsewhere in the program.
- Added in Node.js 21.7 and 20.12.