Skip to content

Commit 2795fce

Browse files
committed
reorganize faq
1 parent d5495d4 commit 2795fce

1 file changed

Lines changed: 53 additions & 66 deletions

File tree

README.md

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

80-
Need to pass options like `quiet: true`? See the [FAQ below](#how-do-i-specify-config-options-with-es6-import) for common patterns. 😊
81-
8280
</details>
8381
<details><summary>bun</summary><br>
8482

@@ -425,70 +423,6 @@ There are two alternatives to this approach:
425423
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)
426424
</details>
427425

428-
<details><summary>How do I specify config options with ES6 import?</summary><br/>
429-
430-
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:
431-
432-
**Option 1: Import and call `config()` yourself (Recommended)**
433-
434-
```javascript
435-
// index.mjs
436-
import dotenv from 'dotenv'
437-
438-
dotenv.config({
439-
quiet: true,
440-
path: '/custom/path/to/.env',
441-
debug: true
442-
})
443-
444-
// Now import everything else
445-
import express from 'express'
446-
```
447-
448-
⚠️ **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`.
449-
450-
**Option 2: Use environment variables**
451-
452-
```bash
453-
DOTENV_CONFIG_QUIET=true DOTENV_CONFIG_PATH=/custom/path/to/.env node index.mjs
454-
```
455-
456-
Then in your code you can keep the shorthand:
457-
458-
```javascript
459-
import 'dotenv/config'
460-
```
461-
462-
**Option 3: A tiny wrapper file**
463-
464-
Create `load-env.mjs`:
465-
466-
```javascript
467-
import dotenv from 'dotenv'
468-
dotenv.config({ quiet: true })
469-
```
470-
471-
Then in your main file:
472-
473-
```javascript
474-
import './load-env.mjs'
475-
import express from 'express'
476-
```
477-
478-
Not the most elegant, but it works reliably when hoisting gets in the way.
479-
480-
**Quick reference for config options:**
481-
482-
| Option | Type | Description |
483-
|--------|------|-------------|
484-
| `path` | string | Path to your `.env` file |
485-
| `encoding` | string | File encoding (default: `utf8`) |
486-
| `debug` | boolean | Turn on debug logging |
487-
| `override` | boolean | Override existing env vars |
488-
| `quiet` | boolean | Suppress all console output |
489-
490-
</details>
491-
492426
<details><summary>Can I customize/write plugins for dotenv?</summary><br/>
493427

494428
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:
@@ -538,6 +472,59 @@ npm i -g @dotenvx/dotenvx
538472
dotenvx precommit --install
539473
```
540474

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+
import dotenv from 'dotenv'
486+
487+
dotenv.config({
488+
path: '/custom/path/to/.env',
489+
debug: true
490+
})
491+
492+
// Now import everything else
493+
import express from '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`.
497+
498+
**Option 2: Use environment variables**
499+
500+
```bash
501+
DOTENV_CONFIG_DEBUG=true DOTENV_CONFIG_PATH=/custom/path/to/.env node index.mjs
502+
```
503+
504+
Then in your code you can keep the shorthand:
505+
506+
```javascript
507+
import 'dotenv/config'
508+
```
509+
510+
**Option 3: A tiny wrapper file**
511+
512+
Create `load-env.mjs`:
513+
514+
```javascript
515+
import dotenv from 'dotenv'
516+
dotenv.config({ path: '/custom/path/to/.env', debug: true })
517+
```
518+
519+
Then in your main file:
520+
521+
```javascript
522+
import './load-env.mjs'
523+
import express from 'express'
524+
```
525+
526+
Not the most elegant, but it works reliably when hoisting gets in the way.
527+
541528
</details>
542529
<details><summary>What happens to environment variables that were already set?</summary><br/>
543530

0 commit comments

Comments
 (0)