Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,13 @@ changes:
Loads the `.env` file into `process.env`. Usage of `NODE_OPTIONS`
in the `.env` file will not have any effect on Node.js.

If a variable is already defined in `process.env`, its existing value takes
precedence and is not overwritten by the value from the file. This also applies
to variables set by a previous call to `process.loadEnvFile()`, so when loading
multiple files, the value from the first file that defines a variable is used.
This differs from passing multiple [`--env-file`][] flags, where values from
later files override those from earlier files.

```cjs
const { loadEnvFile } = require('node:process');
loadEnvFile();
Expand All @@ -2736,6 +2743,22 @@ import { loadEnvFile } from 'node:process';
loadEnvFile();
```

To override existing values, parse the file with [`util.parseEnv()`][] and
assign the result to `process.env`:

```cjs
const { readFileSync } = require('node:fs');
const { parseEnv } = require('node:util');
Object.assign(process.env, parseEnv(readFileSync('.env', 'utf8')));
```

```mjs
import process from 'node:process';
import { readFileSync } from 'node:fs';
import { parseEnv } from 'node:util';
Object.assign(process.env, parseEnv(readFileSync('.env', 'utf8')));
```

## `process.mainModule`

<!-- YAML
Expand Down Expand Up @@ -4632,6 +4655,7 @@ cases:
[`'exit'`]: #event-exit
[`'message'`]: child_process.md#event-message
[`'uncaughtException'`]: #event-uncaughtexception
[`--env-file`]: cli.md#--env-filefile
[`--no-deprecation`]: cli.md#--no-deprecation
[`--permission-audit`]: cli.md#--permission-audit
[`--permission`]: cli.md#--permission
Expand Down Expand Up @@ -4674,6 +4698,7 @@ cases:
[`require.cache`]: modules.md#requirecache
[`require.main`]: modules.md#accessing-the-main-module
[`subprocess.kill()`]: child_process.md#subprocesskillsignal
[`util.parseEnv()`]: util.md#utilparseenvcontent
[`v8.setFlagsFromString()`]: v8.md#v8setflagsfromstringflags
[built-in modules with mandatory `node:` prefix]: modules.md#built-in-modules-with-mandatory-node-prefix
[debugger]: debugger.md
Expand Down
Loading