Skip to content

Commit

Permalink
Updating to always use override flag
Browse files Browse the repository at this point in the history
  • Loading branch information
thanosd committed Feb 12, 2024
1 parent 0aa7818 commit 5ae3ed2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ require('dotenv').config({ path: '/custom/path/to/.env' })

By default, `config` will look for a file called .env in the current working directory.

Pass in multiple files as an array, and they will be parsed in order and combined. The first value set for a variable will win. There is no overriding. The combined result will then be applied to `process.env` (or `options.processEnv`, if set). For this final application the `options.override` flag is respected.
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.

```js

```js
require('dotenv').config({ path: ['.env.local', '.env'] })
Expand Down Expand Up @@ -368,7 +370,7 @@ require('dotenv').config({ debug: process.env.DEBUG })

Default: `false`

Override any environment variables that have already been set on your machine with values from your .env file(s). If multiple files have been provided in `option.path` the override does not apply while the files are being combined (see `options.path`). It applies though at the merging of the combined results with `process.env` (or `optiors.processEnv`, if set).
Override any environment variables that have already been set on your machine with values from your .env file(s). If multiple files have been provided in `option.path` the override will also be used as each file is combined with the next. Without `override` being set, the first value wins. With `override` set the last value wins.

```js
require('dotenv').config({ override: true })
Expand Down
10 changes: 3 additions & 7 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,15 @@ function configDotenv (options) {
// If we have options.path, and it had valid paths, use them. Else fall back to .env
const pathsToProcess = optionPathsThatExist.length ? optionPathsThatExist : [dotenvPath]

// Build the parsed data in a temporary object. As we iterate through the multiple option.path objects,
// we will not want to be overriding any variable after the first time it is discovered. Thus, the
// "options.override" will be false. Once we have the final parsed data, we will populate the process.env
// (or options.processEnv) object with the parsed data, and for that merge, we will consider the "options.override"
// Build the parsed data in a temporary object (because we need to return it). Once we have the final
// parsed data, we will combine it with process.env (or options.processEnv if provided).

const parsed = {}
try {
const mergeOptions = { ...options }
delete mergeOptions.override
for (const path of pathsToProcess) {
// Specifying an encoding returns a string instead of a buffer
const singleFileParsed = DotenvModule.parse(fs.readFileSync(path, { encoding }))
DotenvModule.populate(parsed, singleFileParsed, mergeOptions)
DotenvModule.populate(parsed, singleFileParsed, options)
}

let processEnv = process.env
Expand Down

0 comments on commit 5ae3ed2

Please sign in to comment.