@@ -2305,6 +2305,222 @@ import { loadEnvFile } from 'node:process';
2305
2305
loadEnvFile ();
2306
2306
` ` `
2307
2307
2308
+ ### Environment variables file parser specification
2309
+
2310
+ This section describes how the environment variables file parser reads a file
2311
+ and sets up the environment variables in Node.js.
2312
+
2313
+ 1. **Basic parsing**:
2314
+
2315
+ * The parser reads line by line, splitting each line into a key and a value
2316
+ at the first ` = ` sign.
2317
+ * Lines without an ` = ` are ignored.
2318
+
2319
+ ` ` ` bash
2320
+ BASIC = basic
2321
+ WITHOUT_EQUAL
2322
+ ` ` `
2323
+
2324
+ ` ` ` cjs
2325
+ const assert = require (' node:assert' );
2326
+ const process = require (' node:process' );
2327
+ assert .strictEqual (process .env .BASIC , ' basic' );
2328
+ assert .strictEqual (process .env .WITHOUT_EQUAL , undefined );
2329
+ ` ` `
2330
+
2331
+ ` ` ` mjs
2332
+ import assert from ' node:assert' ;
2333
+ import process from ' node:process' ;
2334
+ assert .strictEqual (process .env .BASIC , ' basic' );
2335
+ assert .strictEqual (process .env .WITHOUT_EQUAL , undefined );
2336
+ ` ` `
2337
+
2338
+ 2. **Comments**:
2339
+
2340
+ * Lines starting with ` #` are treated as comments and ignored.
2341
+ * Inline comments (starting with ` #` after a value) are ignored, and do not
2342
+ affect parsing of the value.
2343
+
2344
+ ` ` ` bash
2345
+ # COMMENTS = work
2346
+ INLINE_COMMENTS = inline comments # work
2347
+ ` ` `
2348
+
2349
+ ` ` ` cjs
2350
+ const assert = require (' node:assert' );
2351
+ const process = require (' node:process' );
2352
+ assert .strictEqual (process .env .COMMENTS , undefined );
2353
+ assert .strictEqual (process .env .INLINE_COMMENTS , ' inline comments' );
2354
+ ` ` `
2355
+
2356
+ ` ` ` mjs
2357
+ import assert from ' node:assert' ;
2358
+ import process from ' node:process' ;
2359
+ assert .strictEqual (process .env .COMMENTS , undefined );
2360
+ assert .strictEqual (process .env .INLINE_COMMENTS , ' inline comments' );
2361
+ ` ` `
2362
+
2363
+ 3. **Whitespace handling**:
2364
+
2365
+ * Leading and trailing whitespaces around keys and values are ignored unless
2366
+ they are enclosed within quotes.
2367
+
2368
+ ` ` ` bash
2369
+ SPACED_KEY = parsed
2370
+ ` ` `
2371
+
2372
+ ` ` ` cjs
2373
+ const assert = require (' node:assert' );
2374
+ const process = require (' node:process' );
2375
+ assert .strictEqual (process .env .SPACED_KEY , ' parsed' );
2376
+ ` ` `
2377
+
2378
+ ` ` ` mjs
2379
+ import assert from ' node:assert' ;
2380
+ import process from ' node:process' ;
2381
+ assert .strictEqual (process .env .SPACED_KEY , ' parsed' );
2382
+ ` ` `
2383
+
2384
+ 4. **Empty values**:
2385
+
2386
+ * Variables with no value assigned (just a key followed by ` = ` ) are set to
2387
+ an empty string.
2388
+
2389
+ ` ` ` bash
2390
+ EMPTY =
2391
+ ` ` `
2392
+
2393
+ ` ` ` cjs
2394
+ const assert = require (' node:assert' );
2395
+ const process = require (' node:process' );
2396
+ assert .strictEqual (process .env .EMPTY , ' ' );
2397
+ ` ` `
2398
+
2399
+ ` ` ` mjs
2400
+ import assert from ' node:assert' ;
2401
+ import process from ' node:process' ;
2402
+ assert .strictEqual (process .env .EMPTY , ' ' );
2403
+ ` ` `
2404
+
2405
+ 5. **Quoted values**:
2406
+
2407
+ * Single (` ' `), double (`"`), and backtick (`` ` ``) quotes are recognized.
2408
+ The content within the quotes is taken as is, including leading and
2409
+ trailing spaces.
2410
+ * Quotes within a different quote type are preserved.
2411
+
2412
+ ```bash
2413
+ FIRST_NAME=' John '
2414
+ MIXED_QUOTES="Say ' Hello! ' "
2415
+ BACKTICK_IN_QUOTES="Using `backticks` in double quotes"
2416
+ ```
2417
+
2418
+ ```cjs
2419
+ const assert = require(' node: assert' );
2420
+ const process = require(' node: process ' );
2421
+ assert.strictEqual(process.env.FIRST_NAME, ' John ' );
2422
+ assert.strictEqual(process.env.MIXED_QUOTES, "Say ' Hello! ' ");
2423
+ assert.strictEqual(process.env.BACKTICK_IN_QUOTES,
2424
+ ' Using ` backticks` in double quotes' );
2425
+ ```
2426
+
2427
+ ```mjs
2428
+ import assert from ' node: assert' ;
2429
+ import process from ' node: process ' ;
2430
+ assert.strictEqual(process.env.FIRST_NAME, ' John ' );
2431
+ assert.strictEqual(process.env.MIXED_QUOTES, "Say ' Hello! ' ");
2432
+ assert.strictEqual(process.env.BACKTICK_IN_QUOTES,
2433
+ ' Using ` backticks` in double quotes' );
2434
+ ```
2435
+
2436
+ 6. **Multiline values**:
2437
+
2438
+ * Values enclosed in double, single, or backtick quotes that span multiple
2439
+ lines are accepted and stored with newline characters.
2440
+
2441
+ ```bash
2442
+ MULTI_DOUBLE_QUOTED="double
2443
+ quoted"
2444
+
2445
+ MULTI_SINGLE_QUOTED=' single
2446
+ quoted'
2447
+
2448
+ MULTI_BACKTICKED=`this
2449
+ "multiline"
2450
+ value`
2451
+ ```
2452
+
2453
+ ```cjs
2454
+ const assert = require(' node: assert' );
2455
+ const process = require(' node: process ' );
2456
+ assert.strictEqual(process.env.MULTI_DOUBLE_QUOTED, ' double\nquoted' );
2457
+ assert.strictEqual(process.env.MULTI_SINGLE_QUOTED, ' single\nquoted' );
2458
+ assert.strictEqual(process.env.MULTI_BACKTICKED,
2459
+ ' this \n" multiline" \nvalue' );
2460
+ ```
2461
+
2462
+ ```mjs
2463
+ import assert from ' node: assert' ;
2464
+ import process from ' node: process ' ;
2465
+ assert.strictEqual(process.env.MULTI_DOUBLE_QUOTED, ' double\nquoted' );
2466
+ assert.strictEqual(process.env.MULTI_SINGLE_QUOTED, ' single\nquoted' );
2467
+ assert.strictEqual(process.env.MULTI_BACKTICKED,
2468
+ ' this \n" multiline" \nvalue' );
2469
+ ```
2470
+
2471
+ 7. **Escapes**:
2472
+
2473
+ * Newlines in double-quoted values are expanded to newline characters.
2474
+ * Other escapes (e.g., `\n `) are treated as literal text in single-quoted
2475
+ or unquoted values.
2476
+
2477
+ ```bash
2478
+ EXPAND_NEWLINES="expand\n new\n lines"
2479
+ DONT_EXPAND_UNQUOTED=dontexpand\n newlines
2480
+ DONT_EXPAND_SQUOTED=' dontexpand\nnewlines'
2481
+ ```
2482
+
2483
+ ```cjs
2484
+ const assert = require(' node: assert' );
2485
+ const process = require(' node: process ' );
2486
+ assert.strictEqual(process.env.EXPAND_NEWLINES, ' expand\nnew\nlines' );
2487
+ assert.strictEqual(process.env.DONT_EXPAND_UNQUOTED,
2488
+ ' dontexpand\\nnewlines' );
2489
+ assert.strictEqual(process.env.DONT_EXPAND_SQUOTED,
2490
+ ' dontexpand\\nnewlines' );
2491
+ ```
2492
+
2493
+ ```mjs
2494
+ import assert from ' node: assert' ;
2495
+ import process from ' node: process ' ;
2496
+ assert.strictEqual(process.env.EXPAND_NEWLINES, ' expand\nnew\nlines' );
2497
+ assert.strictEqual(process.env.DONT_EXPAND_UNQUOTED,
2498
+ ' dontexpand\\nnewlines' );
2499
+ assert.strictEqual(process.env.DONT_EXPAND_SQUOTED,
2500
+ ' dontexpand\\nnewlines' );
2501
+ ```
2502
+
2503
+ 8. **Export statements**:
2504
+
2505
+ * Any `export` keyword before a key is ignored, allowing compatibility with
2506
+ shell scripts.
2507
+
2508
+ ```bash
2509
+ export EXPORT_EXAMPLE = ignore export
2510
+ ```
2511
+
2512
+ ```cjs
2513
+ const assert = require(' node: assert' );
2514
+ const process = require(' node: process ' );
2515
+ assert.strictEqual(process.env.EXPORT_EXAMPLE, ' ignore export ' );
2516
+ ```
2517
+
2518
+ ```mjs
2519
+ import assert from ' node : assert ' ;
2520
+ import process from ' node : process ' ;
2521
+ assert.strictEqual(process.env.EXPORT_EXAMPLE, ' ignore export ' );
2522
+ ```
2523
+
2308
2524
## `process.mainModule`
2309
2525
2310
2526
<!-- YAML
0 commit comments