Skip to content

Commit c723fb0

Browse files
committed
src: fix tests and update doc
1 parent 6c03fd4 commit c723fb0

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed

doc/api/cli.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,35 @@ your environment files.
786786
node --env-file=.env --env-file-override-local index.js
787787
```
788788

789-
To override variables during runtime, use [`process.loadEnvFile({ override: true })`][]
789+
Example:
790+
791+
Assume you have a local environment variable on your machine
792+
793+
```bash
794+
API_KEY="local-key"
795+
```
796+
797+
Meanwhile, your `.env` file contains:
798+
799+
```bash
800+
API_KEY='env-custom-key'
801+
```
802+
803+
If you want to use the value from the `.env` file, you should add the
804+
`--env-file-override-local` flag when running your application.
805+
This flag requires the use of the `--env-file` flag to load environment files.
806+
807+
```js
808+
console.log(process.env.API_KEY); // 'env-custom-key'
809+
```
810+
811+
If you prefer to keep the local value, do not add the
812+
`--env-file-override-local` flag or avoid providing the override option to
813+
[`process.loadEnvFile()`][]
814+
815+
```js
816+
console.log(process.env.API_KEY); // 'local-key'
817+
```
790818

791819
### `-e`, `--eval "script"`
792820

@@ -3227,7 +3255,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
32273255
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
32283256
[`import` specifier]: esm.md#import-specifiers
32293257
[`net.getDefaultAutoSelectFamilyAttemptTimeout()`]: net.md#netgetdefaultautoselectfamilyattempttimeout
3230-
[`process.loadEnvFile({ override: true })`]: process.md#processloadenvfilepath-options
3258+
[`process.loadEnvFile()`]: process.md#processloadenvfilepath-options
32313259
[`process.setUncaughtExceptionCaptureCallback()`]: process.md#processsetuncaughtexceptioncapturecallbackfn
32323260
[`process.setuid()`]: process.md#processsetuidid
32333261
[`setuid(2)`]: https://man7.org/linux/man-pages/man2/setuid.2.html

doc/api/process.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,41 +2307,40 @@ changes:
23072307
Loads the `.env` file into `process.env`. Usage of `NODE_OPTIONS`
23082308
in the `.env` file will not have any effect on Node.js.
23092309
2310-
```cjs
2311-
const { loadEnvFile } = require('node:process');
2312-
loadEnvFile();
2313-
```
2314-
2315-
```mjs
2316-
import { loadEnvFile } from 'node:process';
2317-
loadEnvFile();
2310+
```js
2311+
process.loadEnvFile();
23182312
```
23192313
23202314
Override local environment variables using override option
23212315
2322-
```cjs
2323-
const { loadEnvFile } = require('node:process');
2324-
loadEnvFile('.env', { override: true });
2316+
Example:
2317+
2318+
Assume you have a local environment variable on your machine
2319+
2320+
```bash
2321+
API_KEY="local-key"
23252322
```
23262323
2327-
```mjs
2328-
import { loadEnvFile } from 'node:process';
2329-
loadEnvFile('.env', { override: true });
2324+
Meanwhile, your `.env` file contains:
2325+
2326+
```bash
2327+
API_KEY='env-custom-key'
23302328
```
23312329
2332-
This API is available through the [`--env-file-override-local`][] flag.
2330+
If you want to use the value from the `.env` file
2331+
2332+
```js
2333+
process.loadEnvFile('.env', { override: true });
2334+
console.log(process.env.API_KEY); // 'env-custom-key'
2335+
```
23332336
23342337
Load environment variables with options only
23352338
2336-
```cjs
2337-
const { loadEnvFile } = require('node:process');
2338-
loadEnvFile({ override: true });
2339+
```js
2340+
process.loadEnvFile({ override: true });
23392341
```
23402342
2341-
```mjs
2342-
import { loadEnvFile } from 'node:process';
2343-
loadEnvFile({ override: true });
2344-
```
2343+
This API is available through the [`--env-file-override-local`][] flag.
23452344
23462345
## `process.mainModule`
23472346

src/node_process_methods.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,8 @@ static void ReallyExit(const FunctionCallbackInfo<Value>& args) {
471471
static void LoadEnvFile(const v8::FunctionCallbackInfo<v8::Value>& args) {
472472
Environment* env = Environment::GetCurrent(args);
473473

474-
CHECK_EQ(args.Length(), 2);
474+
CHECK_EQ(args.Length(), 2); // file_path, [should_override]
475475
CHECK(args[0]->IsString());
476-
CHECK(args[1]->IsBoolean());
477476

478477
Utf8Value path_value(args.GetIsolate(), args[0]);
479478
std::string path = path_value.ToString();

test/parallel/test-process-load-env-file.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const common = require('../common');
44
const fixtures = require('../../test/common/fixtures');
55
const assert = require('node:assert');
66
const { describe, it } = require('node:test');
7-
const { join } = require('node:path');
7+
const { join, toNamespacedPath } = require('node:path');
88

99
const basicValidEnvFilePath = fixtures.path('dotenv/basic-valid.env');
1010
const validEnvFilePath = fixtures.path('dotenv/valid.env');
@@ -64,7 +64,7 @@ describe('process.loadEnvFile()', () => {
6464

6565
assert.throws(() => {
6666
process.loadEnvFile();
67-
}, { code: 'ENOENT', syscall: 'open', path: '.env' });
67+
}, { code: 'ENOENT', syscall: 'open', path: toNamespacedPath('.env') });
6868
} finally {
6969
if (common.isMainThread) {
7070
process.chdir(originalCwd);
@@ -106,10 +106,9 @@ describe('process.loadEnvFile()', () => {
106106
});
107107
});
108108

109-
describe('process.loadEnvFile(path, { override: true })', () => {
109+
describe('process.loadEnvFile(path, options)', () => {
110110

111111
it('should not override the original value', async () => {
112-
process.env.BASIC = 'Original value';
113112
const code = `
114113
process.loadEnvFile(${JSON.stringify(validEnvFilePath)});
115114
const assert = require('assert');
@@ -118,39 +117,39 @@ describe('process.loadEnvFile(path, { override: true })', () => {
118117
const child = await common.spawnPromisified(
119118
process.execPath,
120119
[ '--eval', code ],
121-
{ cwd: __dirname },
120+
{ cwd: __dirname, env: { ...process.env, BASIC: 'Original value' } },
122121
);
123122
assert.strictEqual(child.stderr, '');
124123
assert.strictEqual(child.code, 0);
125124
});
126125

127126
it('should override the original value', async () => {
128-
process.env.BASIC = 'Original value';
129127
const code = `
130-
process.loadEnvFile(${JSON.stringify(validEnvFilePath)}, {override: true});
131128
const assert = require('assert');
129+
assert.strictEqual(process.env.BASIC, 'Original value');
130+
process.loadEnvFile(${JSON.stringify(validEnvFilePath)}, {override: true});
132131
assert.strictEqual(process.env.BASIC, 'basic');
133132
`.trim();
134133
const child = await common.spawnPromisified(
135134
process.execPath,
136135
[ '--eval', code ],
137-
{ cwd: __dirname },
136+
{ cwd: __dirname, env: { ...process.env, BASIC: 'Original value' } },
138137
);
139138
assert.strictEqual(child.stderr, '');
140139
assert.strictEqual(child.code, 0);
141140
});
142141

143142
it('supports passing options only', async () => {
144-
process.env.BASIC = 'Original value';
145143
const code = `
146-
process.loadEnvFile({override: true});
147144
const assert = require('assert');
145+
assert.strictEqual(process.env.BASIC, 'Original value');
146+
process.loadEnvFile({override: true});
148147
assert.strictEqual(process.env.BASIC, 'basic');
149148
`.trim();
150149
const child = await common.spawnPromisified(
151150
process.execPath,
152151
[ '--eval', code ],
153-
{ cwd: fixtures.path('dotenv/') },
152+
{ cwd: fixtures.path('dotenv/'), env: { ...process.env, BASIC: 'Original value' } },
154153
);
155154
assert.strictEqual(child.stderr, '');
156155
assert.strictEqual(child.code, 0);

0 commit comments

Comments
 (0)