From 13d9b53909e2e4b7ec8bd0d0561601222ab1cae4 Mon Sep 17 00:00:00 2001 From: YCM Jason Date: Fri, 17 Oct 2025 08:22:49 +0100 Subject: [PATCH 01/11] Document process.loadEnvFile for loading .env files Added information about loading .env files programmatically in Node.js using process.loadEnvFile. Signed-off-by: YCM Jason --- ...-read-environment-variables-from-nodejs.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index 12bd03360d9ca..2a56441a5331e 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -64,3 +64,42 @@ throwing an error if the file is missing using the `--env-file-if-exists` flag. ```bash node --env-file-if-exists=.env app.js ``` + +## Loading `.env` files programmatically with `process.loadEnvFile(path)` + +As of **Node.js v21.7.0** (and **v20.12.0** for Node 20 LTS), Node.js provides a built-in API to load `.env` files directly from your code: [`process.loadEnvFile(path)`](https://nodejs.org/api/process.html#processloadenvfilepath). + +This method loads variables from a `.env` file into `process.env`, similar to how the `--env-file` flag works — but can be invoked programmatically. + +### Example + +```js +const { loadEnvFile } = require('node:process'); + +// Loads environment variables from the default .env file +loadEnvFile(); + +console.log(process.env.PORT); +``` + +You can also specify a custom path: + +```js +const { loadEnvFile } = require('node:process'); +loadEnvFile('./config/.env'); +``` + +### Notes + +- **Parameters:** + `path` can be a string, `URL`, `Buffer`, or `undefined`. + Default: `'./.env'`. + +- **Version history:** + - Added in: `v21.7.0`, `v20.12.0` + - No longer experimental as of `v24.10.0` + +- **Behaviour:** + Values from `.env` are merged into `process.env`. + Environment variables already set in the process take precedence. + Variables like `NODE_OPTIONS` inside `.env` have no effect on Node.js startup behavior. From 0a67137e7e1b68d6a233f8ef8fe505bf472a765e Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Fri, 17 Oct 2025 08:29:06 +0100 Subject: [PATCH 02/11] leave a signature at authors --- .../how-to-read-environment-variables-from-nodejs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index 2a56441a5331e..efb7039f2d74c 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -1,7 +1,7 @@ --- title: How to read environment variables from Node.js layout: learn -authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, manishprivet, nikhilbhatt +authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, manishprivet, nikhilbhatt, ycmjason --- # How to read environment variables from Node.js From f74ae1ff526f89cea45d1b6673a1ca63a7044b2b Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Fri, 17 Oct 2025 08:30:59 +0100 Subject: [PATCH 03/11] american english --- .../how-to-read-environment-variables-from-nodejs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index efb7039f2d74c..5cd43fa004345 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -99,7 +99,7 @@ loadEnvFile('./config/.env'); - Added in: `v21.7.0`, `v20.12.0` - No longer experimental as of `v24.10.0` -- **Behaviour:** +- **Behavior:** Values from `.env` are merged into `process.env`. Environment variables already set in the process take precedence. Variables like `NODE_OPTIONS` inside `.env` have no effect on Node.js startup behavior. From cf10c2f33fc12ef754baa94fefa9f4a7b8fb733f Mon Sep 17 00:00:00 2001 From: YCM Jason Date: Sat, 18 Oct 2025 08:48:52 +0100 Subject: [PATCH 04/11] Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller Signed-off-by: YCM Jason --- .../how-to-read-environment-variables-from-nodejs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index 5cd43fa004345..6e56b1cc4cbbc 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -67,7 +67,7 @@ node --env-file-if-exists=.env app.js ## Loading `.env` files programmatically with `process.loadEnvFile(path)` -As of **Node.js v21.7.0** (and **v20.12.0** for Node 20 LTS), Node.js provides a built-in API to load `.env` files directly from your code: [`process.loadEnvFile(path)`](https://nodejs.org/api/process.html#processloadenvfilepath). +Node.js provides a built-in API to load `.env` files directly from your code: [`process.loadEnvFile(path)`](https://nodejs.org/api/process.html#processloadenvfilepath). This method loads variables from a `.env` file into `process.env`, similar to how the `--env-file` flag works — but can be invoked programmatically. From 4bb62833290d095cfc3d0a685a9903b634d8d596 Mon Sep 17 00:00:00 2001 From: YCM Jason Date: Sat, 18 Oct 2025 08:49:07 +0100 Subject: [PATCH 05/11] Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller Signed-off-by: YCM Jason --- ...ow-to-read-environment-variables-from-nodejs.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index 6e56b1cc4cbbc..b168e80dc4182 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -89,17 +89,3 @@ const { loadEnvFile } = require('node:process'); loadEnvFile('./config/.env'); ``` -### Notes - -- **Parameters:** - `path` can be a string, `URL`, `Buffer`, or `undefined`. - Default: `'./.env'`. - -- **Version history:** - - Added in: `v21.7.0`, `v20.12.0` - - No longer experimental as of `v24.10.0` - -- **Behavior:** - Values from `.env` are merged into `process.env`. - Environment variables already set in the process take precedence. - Variables like `NODE_OPTIONS` inside `.env` have no effect on Node.js startup behavior. From 66707a0f79de2e768233aaac41805bc89df09bf9 Mon Sep 17 00:00:00 2001 From: YCM Jason Date: Sat, 18 Oct 2025 08:49:34 +0100 Subject: [PATCH 06/11] Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller Signed-off-by: YCM Jason --- .../how-to-read-environment-variables-from-nodejs.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index b168e80dc4182..4a6faab397c13 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -73,6 +73,10 @@ This method loads variables from a `.env` file into `process.env`, similar to ho ### Example +```env +PORT=1234 +``` + ```js const { loadEnvFile } = require('node:process'); From 553f750623ec52a9b9aa8280031821891ab23519 Mon Sep 17 00:00:00 2001 From: YCM Jason Date: Sat, 18 Oct 2025 08:49:43 +0100 Subject: [PATCH 07/11] Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller Signed-off-by: YCM Jason --- .../how-to-read-environment-variables-from-nodejs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index 4a6faab397c13..8c04e2c720417 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -83,7 +83,7 @@ const { loadEnvFile } = require('node:process'); // Loads environment variables from the default .env file loadEnvFile(); -console.log(process.env.PORT); +console.log(process.env.PORT); // Logs '1234' ``` You can also specify a custom path: From 5f1ee4740bd4872c87d694dca6b84dfd70b119d3 Mon Sep 17 00:00:00 2001 From: YCM Jason Date: Sat, 18 Oct 2025 08:50:06 +0100 Subject: [PATCH 08/11] Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller Signed-off-by: YCM Jason --- .../how-to-read-environment-variables-from-nodejs.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index 8c04e2c720417..eea7cc63cd272 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -71,6 +71,8 @@ Node.js provides a built-in API to load `.env` files directly from your code: [` This method loads variables from a `.env` file into `process.env`, similar to how the `--env-file` flag works — but can be invoked programmatically. +Because this method is invoked post-initialization, the setting of startup-related environment variables (i.e. `NODE_OPTIONS`) has no effect on the process (however, these variables can still be accessed via `process.env`). + ### Example ```env From f0d6748894bcd7bcaa2840ff78d9d922380b2396 Mon Sep 17 00:00:00 2001 From: YCM Jason Date: Sun, 19 Oct 2025 10:05:15 +0100 Subject: [PATCH 09/11] Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com> Signed-off-by: YCM Jason --- .../how-to-read-environment-variables-from-nodejs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index eea7cc63cd272..6772ac775b9ae 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -75,7 +75,7 @@ Because this method is invoked post-initialization, the setting of startup-relat ### Example -```env +```txt PORT=1234 ``` From 500afd25ec56bb89c9949926c09d99770cb18e66 Mon Sep 17 00:00:00 2001 From: YCM Jason Date: Sun, 19 Oct 2025 12:46:47 +0100 Subject: [PATCH 10/11] Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Michael Esteban Signed-off-by: YCM Jason --- .../how-to-read-environment-variables-from-nodejs.md | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index 6772ac775b9ae..efc0e5591da19 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -76,6 +76,7 @@ Because this method is invoked post-initialization, the setting of startup-relat ### Example ```txt +// .env file PORT=1234 ``` From f57521f34b139a6c9a5e2dccdb5e3a8b91201c4b Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Sun, 19 Oct 2025 20:21:11 +0100 Subject: [PATCH 11/11] lint --- .../how-to-read-environment-variables-from-nodejs.md | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md index efc0e5591da19..9afb46f342ba1 100644 --- a/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md +++ b/apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md @@ -95,4 +95,3 @@ You can also specify a custom path: const { loadEnvFile } = require('node:process'); loadEnvFile('./config/.env'); ``` -