Skip to content
Tutorials Center
My learning

Topics

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.

NODE
process.loadEnvFile('./.env');
console.log(process.env.PORT);

Try it

Node.js Load a file into process.env

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.

Node.js parseEnv and errors

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

NODE
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=value per line. Lines starting with # are comments, and # after a value starts a comment too. An optional export prefix is accepted. Values may be wrapped in single quotes, double quotes or backticks.
  • Quotes and newlines. In double quotes \n becomes a newline; single quotes keep the text literally. Quoted values can span several lines.
  • No variable expansion. A=$B is the literal text, not the value of B.
  • Everything is a string. PORT=3000 gives '3000'. Convert with Number() 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.js loads the file before any code runs, so it also affects module-level code and Node options in NODE_OPTIONS. --env-file-if-exists ignores a missing file. Use loadEnvFile() when you want to decide at runtime.
  • Do not commit secrets. Add .env to .gitignore and 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.