Skip to content

Commit 44ad1e2

Browse files
ycmjasonavivkellerAugustinMauroymikeesto
authored
Document process.loadEnvFile for loading .env files (#8233)
* 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 <me@ycmjason.com> * leave a signature at authors * american english * Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller <me@aviv.sh> Signed-off-by: YCM Jason <me@ycmjason.com> * Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller <me@aviv.sh> Signed-off-by: YCM Jason <me@ycmjason.com> * Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller <me@aviv.sh> Signed-off-by: YCM Jason <me@ycmjason.com> * Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller <me@aviv.sh> Signed-off-by: YCM Jason <me@ycmjason.com> * Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Aviv Keller <me@aviv.sh> Signed-off-by: YCM Jason <me@ycmjason.com> * 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 <me@ycmjason.com> * Update apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md Co-authored-by: Michael Esteban <mickel13@gmail.com> Signed-off-by: YCM Jason <me@ycmjason.com> * lint --------- Signed-off-by: YCM Jason <me@ycmjason.com> Co-authored-by: Aviv Keller <me@aviv.sh> Co-authored-by: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com> Co-authored-by: Michael Esteban <mickel13@gmail.com>
1 parent 7015095 commit 44ad1e2

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

apps/site/pages/en/learn/command-line/how-to-read-environment-variables-from-nodejs.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: How to read environment variables from Node.js
33
layout: learn
4-
authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, manishprivet, nikhilbhatt
4+
authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, manishprivet, nikhilbhatt, ycmjason
55
---
66

77
# How to read environment variables from Node.js
@@ -64,3 +64,34 @@ throwing an error if the file is missing using the `--env-file-if-exists` flag.
6464
```bash
6565
node --env-file-if-exists=.env app.js
6666
```
67+
68+
## Loading `.env` files programmatically with `process.loadEnvFile(path)`
69+
70+
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).
71+
72+
This method loads variables from a `.env` file into `process.env`, similar to how the `--env-file` flag works — but can be invoked programmatically.
73+
74+
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`).
75+
76+
### Example
77+
78+
```txt
79+
// .env file
80+
PORT=1234
81+
```
82+
83+
```js
84+
const { loadEnvFile } = require('node:process');
85+
86+
// Loads environment variables from the default .env file
87+
loadEnvFile();
88+
89+
console.log(process.env.PORT); // Logs '1234'
90+
```
91+
92+
You can also specify a custom path:
93+
94+
```js
95+
const { loadEnvFile } = require('node:process');
96+
loadEnvFile('./config/.env');
97+
```

0 commit comments

Comments
 (0)