You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-73Lines changed: 62 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,21 +22,23 @@ Create a `.env` file in the root of your project:
22
22
23
23
```ini
24
24
# .env
25
-
S3_BUCKET="YOURS3BUCKET"
26
-
SECRET_KEY="YOURSECRETKEYGOESHERE"
25
+
HELLO="Dotenv"
26
+
OPENAI_API_KEY="your-api-key-goes-here"
27
27
```
28
28
29
-
And as early as possible in your application, import and configure dotenv:
29
+
As early as possible in your application, import and configure dotenv:
30
30
31
31
```javascript
32
32
// index.js
33
-
require('dotenv').config() // or import 'dotenv/config' if you're using ES6
34
-
...
35
-
console.log(process.env) // remove this after you've confirmed it is working
33
+
require('dotenv').config()
34
+
// or import 'dotenv/config' // for esm
35
+
36
+
console.log(`Hello ${process.env.HELLO}`)
36
37
```
37
38
```sh
38
39
$ node index.js
39
-
◇ injected env (14) from .env
40
+
◇ injected env (2) from .env
41
+
Hello Dotenv
40
42
```
41
43
42
44
That's it. `process.env` now has the keys and values you defined in your `.env` file.
@@ -75,8 +77,6 @@ import dotenv from 'dotenv'
75
77
dotenv.config({ path:'/custom/path/to/.env' })
76
78
```
77
79
78
-
Need to pass options like `quiet: true`? See the [FAQ below](#how-do-i-specify-config-options-with-es6-import) for common patterns. 😊
79
-
80
80
</details>
81
81
<details><summary>bun</summary><br>
82
82
@@ -423,70 +423,6 @@ There are two alternatives to this approach:
423
423
2. Create a separate file that will execute `config` first as outlined in [this comment on #133](https://github.com/motdotla/dotenv/issues/133#issuecomment-255298822)
424
424
</details>
425
425
426
-
<details><summary>How do I specify config options with ES6 import?</summary><br/>
427
-
428
-
This trips up a lot of folks (myself included the first time 😅). When using `import 'dotenv/config'`, you can't pass options directly. Here are a few practical ways to handle it:
429
-
430
-
**Option 1: Import and call `config()` yourself (Recommended)**
431
-
432
-
```javascript
433
-
// index.mjs
434
-
importdotenvfrom'dotenv'
435
-
436
-
dotenv.config({
437
-
quiet:true,
438
-
path:'/custom/path/to/.env',
439
-
debug:true
440
-
})
441
-
442
-
// Now import everything else
443
-
importexpressfrom'express'
444
-
```
445
-
446
-
⚠️ **Heads up:** Because ES6 imports are hoisted, put the `dotenv` import and `config()` call at the very top, before any other imports that rely on `process.env`.
|`override`| boolean | Override existing env vars |
486
-
|`quiet`| boolean | Suppress all console output |
487
-
488
-
</details>
489
-
490
426
<details><summary>Can I customize/write plugins for dotenv?</summary><br/>
491
427
492
428
Yes! `dotenv.config()` returns an object representing the parsed `.env` file. This gives you everything you need to continue setting values on `process.env`. For example:
@@ -536,6 +472,59 @@ npm i -g @dotenvx/dotenvx
536
472
dotenvx precommit --install
537
473
```
538
474
475
+
</details>
476
+
477
+
<details><summary>How do I specify config options with ES6 import?</summary><br/>
478
+
479
+
When using `import 'dotenv/config'`, you can't pass options directly. Here are a few ways to handle it.
480
+
481
+
**Option 1: Import and call `config()` yourself (Recommended)**
482
+
483
+
```javascript
484
+
// index.mjs
485
+
importdotenvfrom'dotenv'
486
+
487
+
dotenv.config({
488
+
path:'/custom/path/to/.env',
489
+
debug:true
490
+
})
491
+
492
+
// Now import everything else
493
+
importexpressfrom'express'
494
+
```
495
+
496
+
Because ES6 imports are hoisted, put the `dotenv` import and `config()` call at the very top, before any other imports that rely on `process.env`.
0 commit comments