Skip to content

Commit 8197a16

Browse files
authored
Merge pull request #1008 from vincent067/docs/esm-config-options
docs: add FAQ for passing config options with ES6 imports
2 parents 48aa216 + b915319 commit 8197a16

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ import dotenv from 'dotenv'
7575
dotenv.config({ path: '/custom/path/to/.env' })
7676
```
7777

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+
7880
</details>
7981
<details><summary>bun</summary><br>
8082

@@ -421,6 +423,70 @@ There are two alternatives to this approach:
421423
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)
422424
</details>
423425

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+
import dotenv from '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+
import express from '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`.
447+
448+
**Option 2: Use environment variables**
449+
450+
```bash
451+
DOTENV_CONFIG_QUIET=true DOTENV_CONFIG_PATH=/custom/path/to/.env node index.mjs
452+
```
453+
454+
Then in your code you can keep the shorthand:
455+
456+
```javascript
457+
import 'dotenv/config'
458+
```
459+
460+
**Option 3: A tiny wrapper file**
461+
462+
Create `load-env.mjs`:
463+
464+
```javascript
465+
import dotenv from 'dotenv'
466+
dotenv.config({ quiet: true })
467+
```
468+
469+
Then in your main file:
470+
471+
```javascript
472+
import './load-env.mjs'
473+
import express from 'express'
474+
```
475+
476+
Not the most elegant, but it works reliably when hoisting gets in the way.
477+
478+
**Quick reference for config options:**
479+
480+
| Option | Type | Description |
481+
|--------|------|-------------|
482+
| `path` | string | Path to your `.env` file |
483+
| `encoding` | string | File encoding (default: `utf8`) |
484+
| `debug` | boolean | Turn on debug logging |
485+
| `override` | boolean | Override existing env vars |
486+
| `quiet` | boolean | Suppress all console output |
487+
488+
</details>
489+
424490
<details><summary>Can I customize/write plugins for dotenv?</summary><br/>
425491

426492
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:

0 commit comments

Comments
 (0)