diff --git a/README.md b/README.md index faa1f2d2..c948b2bf 100644 --- a/README.md +++ b/README.md @@ -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'] }) @@ -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 }) diff --git a/lib/main.js b/lib/main.js index c5dab345..22612d59 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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