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
@@ -534,17 +534,9 @@ Override any environment variables that have already been set.
534
534
## FAQ
535
535
536
536
<details>
537
-
<summary>Why is the `.env` file not loading my environment variables successfully?</summary>
538
-
539
-
Most likely your `.env` file is not in the correct place. [See this stack overflow](https://stackoverflow.com/questions/42335016/dotenv-file-is-not-loading-environment-variables).
540
-
541
-
Turn on debug mode and try again..
542
-
543
-
```js
544
-
require('dotenv').config({ debug:true })
545
-
```
537
+
<summary>What about variable expansion?</summary>
546
538
547
-
You will receive a helpful error outputted to your console.
@@ -556,6 +548,64 @@ passwords or API keys. Your production database should have a different
556
548
password than your development database.
557
549
</details>
558
550
551
+
<details>
552
+
<summary>How do I use dotenv with `import`?</summary>
553
+
554
+
Simply..
555
+
556
+
```javascript
557
+
// index.mjs (ESM)
558
+
import'dotenv/config'// see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
559
+
importexpressfrom'express'
560
+
```
561
+
562
+
A little background..
563
+
564
+
> When you run a module containing an `import` declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.
565
+
>
566
+
> – [ES6 In Depth: Modules](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/)
567
+
568
+
What does this mean in plain language? It means you would think the following would work but it won't.
569
+
570
+
`errorReporter.mjs`:
571
+
```js
572
+
classClient {
573
+
constructor (apiKey) {
574
+
console.log('apiKey', apiKey)
575
+
576
+
this.apiKey= apiKey
577
+
}
578
+
}
579
+
580
+
exportdefaultnewClient(process.env.API_KEY)
581
+
```
582
+
`index.mjs`:
583
+
```js
584
+
// Note: this is INCORRECT and will not work
585
+
import*asdotenvfrom'dotenv'
586
+
dotenv.config()
587
+
588
+
importerrorReporterfrom'./errorReporter.mjs'// process.env.API_KEY will be blank!
589
+
```
590
+
591
+
`process.env.API_KEY` will be blank.
592
+
593
+
Instead, `index.mjs` should be written as..
594
+
595
+
```js
596
+
import'dotenv/config'
597
+
598
+
importerrorReporterfrom'./errorReporter.mjs'
599
+
```
600
+
601
+
Does that make sense? It's a bit unintuitive, but it is how importing of ES6 modules work. Here is a [working example of this pitfall](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-es6-import-pitfall).
602
+
603
+
There are two alternatives to this approach:
604
+
605
+
1. Preload with dotenvx: `dotenvx run -- node index.js` (_Note: you do not need to `import` dotenv with this approach_)
606
+
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)
607
+
</details>
608
+
559
609
<details>
560
610
<summary>Should I have multiple `.env` files?</summary>
561
611
@@ -566,6 +616,19 @@ We recommend creating one `.env` file per environment. Use `.env` for local/deve
<summary>Can I customize/write plugins for dotenv?</summary>
621
+
622
+
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:
623
+
624
+
```js
625
+
constdotenv=require('dotenv')
626
+
constvariableExpansion=require('dotenv-expand')
627
+
constmyEnv=dotenv.config()
628
+
variableExpansion(myEnv)
629
+
```
630
+
</details>
631
+
569
632
<details>
570
633
<summary>What rules does the parsing engine follow?</summary>
571
634
@@ -591,96 +654,71 @@ line'}
591
654
</details>
592
655
593
656
<details>
594
-
<summary>What happens to environment variables that were already set?</summary>
595
-
596
-
By default, we will never modify any environment variables that have already been set. In particular, if there is a variable in your `.env` file which collides with one that already exists in your environment, then that variable will be skipped.
597
-
598
-
If instead, you want to override `process.env` use the `override` option.
657
+
<summary>What about syncing and securing .env files?</summary>
599
658
600
-
```javascript
601
-
require('dotenv').config({ override:true })
602
-
```
659
+
Use [dotenvx](https://github.com/dotenvx/dotenvx) to unlock syncing encrypted .env files over git.
603
660
</details>
604
661
605
662
<details>
606
-
<summary>How come my environment variables are not showing up for React?</summary>
607
-
608
-
Your React code is run in Webpack, where the `fs` module or even the `process` global itself are not accessible out-of-the-box. `process.env` can only be injected through Webpack configuration.
663
+
<summary>What if I accidentally commit my `.env` file to code?</summary>
609
664
610
-
If you are using [`react-scripts`](https://www.npmjs.com/package/react-scripts), which is distributed through [`create-react-app`](https://create-react-app.dev/), it has dotenv built in but with a quirk. Preface your environment variables with `REACT_APP_`. See [this stack overflow](https://stackoverflow.com/questions/42182577/is-it-possible-to-use-dotenv-in-a-react-project) for more details.
665
+
Remove it, [remove git history](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository) and then install the [git pre-commit hook](https://github.com/dotenvx/dotenvx#pre-commit) to prevent this from ever happening again.
611
666
612
-
If you are using other frameworks (e.g. Next.js, Gatsby...), you need to consult their documentation for how to inject environment variables into the client.
667
+
```
668
+
brew install dotenvx/brew/dotenvx
669
+
dotenvx precommit --install
670
+
```
613
671
</details>
614
672
615
673
<details>
616
-
<summary>Can I customize/write plugins for dotenv?</summary>
674
+
<summary>What happens to environment variables that were already set?</summary>
617
675
618
-
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:
676
+
By default, we will never modify any environment variables that have already been set. In particular, if there is a variable in your `.env` file which collides with one that already exists in your environment, then that variable will be skipped.
619
677
620
-
```js
621
-
constdotenv=require('dotenv')
622
-
constvariableExpansion=require('dotenv-expand')
623
-
constmyEnv=dotenv.config()
624
-
variableExpansion(myEnv)
678
+
If instead, you want to override `process.env` use the `override` option.
679
+
680
+
```javascript
681
+
require('dotenv').config({ override:true })
625
682
```
626
683
</details>
627
684
628
685
<details>
629
-
<summary>How do I use dotenv with `import`?</summary>
686
+
<summary>How can I prevent committing my `.env` file to a Docker build?</summary>
630
687
631
-
Simply..
688
+
Use the [docker prebuild hook](https://dotenvx.com/docs/features/prebuild).
632
689
633
-
```javascript
634
-
// index.mjs (ESM)
635
-
import'dotenv/config'// see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
636
-
importexpressfrom'express'
690
+
```bash
691
+
# Dockerfile
692
+
...
693
+
RUN curl -fsS https://dotenvx.sh/ | sh
694
+
...
695
+
RUN dotenvx prebuild
696
+
CMD ["dotenvx", "run", "--", "node", "index.js"]
637
697
```
698
+
</details>
638
699
639
-
A little background..
640
-
641
-
> When you run a module containing an `import` declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.
642
-
>
643
-
> – [ES6 In Depth: Modules](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/)
644
-
645
-
What does this mean in plain language? It means you would think the following would work but it won't.
700
+
<details>
701
+
<summary>How come my environment variables are not showing up for React?</summary>
646
702
647
-
`errorReporter.mjs`:
648
-
```js
649
-
classClient {
650
-
constructor (apiKey) {
651
-
console.log('apiKey', apiKey)
703
+
Your React code is run in Webpack, where the `fs` module or even the `process` global itself are not accessible out-of-the-box. `process.env` can only be injected through Webpack configuration.
652
704
653
-
this.apiKey= apiKey
654
-
}
655
-
}
705
+
If you are using [`react-scripts`](https://www.npmjs.com/package/react-scripts), which is distributed through [`create-react-app`](https://create-react-app.dev/), it has dotenv built in but with a quirk. Preface your environment variables with `REACT_APP_`. See [this stack overflow](https://stackoverflow.com/questions/42182577/is-it-possible-to-use-dotenv-in-a-react-project) for more details.
656
706
657
-
exportdefaultnewClient(process.env.API_KEY)
658
-
```
659
-
`index.mjs`:
660
-
```js
661
-
// Note: this is INCORRECT and will not work
662
-
import*asdotenvfrom'dotenv'
663
-
dotenv.config()
707
+
If you are using other frameworks (e.g. Next.js, Gatsby...), you need to consult their documentation for how to inject environment variables into the client.
708
+
</details>
664
709
665
-
importerrorReporterfrom'./errorReporter.mjs'// process.env.API_KEY will be blank!
666
-
```
710
+
<details>
711
+
<summary>Why is the `.env` file not loading my environment variables successfully?</summary>
667
712
668
-
`process.env.API_KEY` will be blank.
713
+
Most likely your `.env` file is not in the correct place. [See this stack overflow](https://stackoverflow.com/questions/42335016/dotenv-file-is-not-loading-environment-variables).
669
714
670
-
Instead, `index.mjs` should be written as..
715
+
Turn on debug mode and try again..
671
716
672
717
```js
673
-
import'dotenv/config'
674
-
675
-
importerrorReporterfrom'./errorReporter.mjs'
718
+
require('dotenv').config({ debug:true })
676
719
```
677
720
678
-
Does that make sense? It's a bit unintuitive, but it is how importing of ES6 modules work. Here is a [working example of this pitfall](https://github.com/dotenv-org/examples/tree/master/usage/dotenv-es6-import-pitfall).
679
-
680
-
There are two alternatives to this approach:
681
-
682
-
1. Preload with dotenvx: `dotenvx run -- node index.js` (_Note: you do not need to `import` dotenv with this approach_)
683
-
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)
721
+
You will receive a helpful error outputted to your console.
684
722
</details>
685
723
686
724
<details>
@@ -723,46 +761,6 @@ module.exports = {
723
761
Alternatively, just use [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack) which does this and more behind the scenes for you.
<summary>What about syncing and securing .env files?</summary>
734
-
735
-
Use [dotenvx](https://github.com/dotenvx/dotenvx) to unlock syncing encrypted .env files over git.
736
-
</details>
737
-
738
-
<details>
739
-
<summary>What if I accidentally commit my `.env` file to code?</summary>
740
-
741
-
Remove it, [remove git history](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository) and then install the [git pre-commit hook](https://github.com/dotenvx/dotenvx#pre-commit) to prevent this from ever happening again.
742
-
743
-
```
744
-
brew install dotenvx/brew/dotenvx
745
-
dotenvx precommit --install
746
-
```
747
-
</details>
748
-
749
-
<details>
750
-
<summary>How can I prevent committing my `.env` file to a Docker build?</summary>
751
-
752
-
Use the [docker prebuild hook](https://dotenvx.com/docs/features/prebuild).
0 commit comments