Skip to content

Commit 2770676

Browse files
committed
docs: clarify config() behavior for missing paths
1 parent 9d93f22 commit 2770676

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ Dotenv exposes four functions:
588588

589589
`config` will read your `.env` file, parse the contents, assign it to
590590
[`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
591-
and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.
591+
and return an Object with a `parsed` key containing the loaded content (if any). If dotenv encounters an error while reading or parsing, it will also return it in an `error` key.
592592

593593
```js
594594
const result = dotenv.config()
@@ -618,6 +618,12 @@ By default, `config` will look for a file called .env in the current working dir
618618

619619
Pass in multiple files as an array, and they will be parsed in order and combined with `process.env` (or `option.processEnv`, if set). The first value set for a variable will win, unless the `options.override` flag is set, in which case the last value set will win. If a value already exists in `process.env` and the `options.override` flag is NOT set, no changes will be made to that value.
620620

621+
If a file in `options.path` cannot be read (for example, it does not exist), dotenv will continue processing subsequent paths.
622+
623+
If one or more paths fail, dotenv will return the error from the last path in the array that failed, so `error` may be set even if other files were processed successfully.
624+
625+
Environment variables from files that were read successfully will still be applied to `process.env` (subject to the `override` option). `parsed` will only contain variables from files that were successfully processed (or be empty if none were processed successfully).
626+
621627
```js
622628
require('dotenv').config({ path: ['.env.local', '.env'] })
623629
```

lib/main.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export interface DotenvConfigOptions {
2828
*
2929
* Specify a custom path if your file containing environment variables is located elsewhere.
3030
* Can also be an array of strings, specifying multiple paths.
31+
* If multiple paths are provided and some cannot be read, dotenv will continue processing subsequent paths.
32+
* If one or more paths fail, dotenv will return the error from the last path in the array that failed,
33+
* so `error` may be set even if other files were processed successfully.
34+
* Environment variables from files that were read successfully will still be applied (subject to the `override` option).
3135
*
3236
* example: `require('dotenv').config({ path: '/custom/path/to/.env' })`
3337
* example: `require('dotenv').config({ path: ['/path/to/first.env', '/path/to/second.env'] })`
@@ -65,6 +69,7 @@ export interface DotenvConfigOptions {
6569
* Default: `false`
6670
*
6771
* Override any environment variables that have already been set on your machine with values from your .env file.
72+
* If multiple files have been provided in `options.path`, the override will be used as each successfully read file is combined with the next.
6873
*
6974
* example: `require('dotenv').config({ override: true })`
7075
*/
@@ -95,7 +100,7 @@ export interface DotenvConfigOutput {
95100
}
96101

97102
type DotenvError = Error & {
98-
code:
103+
code:
99104
| 'MISSING_DATA'
100105
| 'INVALID_DOTENV_KEY'
101106
| 'NOT_FOUND_DOTENV_ENVIRONMENT'

0 commit comments

Comments
 (0)