diff --git a/README.md b/README.md index dc40047..e74de29 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ Use the following properties to configure your instance. * **defaults** (`false`) - Adds support for `dotenv-defaults`. If set to `true`, uses `./.env.defaults`. If a string, uses that location for a defaults file. Read more at [npm](https://www.npmjs.com/package/dotenv-defaults). * **ignoreStub** (`false`) - Override the automatic check whether to stub `process.env`. [Read more here](#user-content-processenv-stubbing--replacing). * **prefix** (`'process.env.'`) - The prefix to use before the name of your env variables. +* **force** (`false`) - This will override existing env variables with anything you pass in from your file. The following example shows how to set any/all arguments. diff --git a/src/index.js b/src/index.js index 5f9aa63..0541cef 100644 --- a/src/index.js +++ b/src/index.js @@ -57,7 +57,7 @@ class Dotenv { const { env, blueprint } = this.getEnvs() Object.keys(blueprint).forEach(key => { - const value = Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key] + const value = !this.config.force && Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key] const isMissing = typeof value === 'undefined' || value === null || (!allowEmptyValues && value === '') diff --git a/test/index.test.js b/test/index.test.js index cdc4e07..42e3665 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -236,6 +236,16 @@ describe.each(versions)('%s', (_, DotenvPlugin) => { }) }) + describe('force configuration', () => { + test('Should override if forced', (done) => { + expectResultsToContainReplacements( + new DotenvPlugin({ force: true }), + { ...defaultEnvResult, ...missingOneResult }, + done + ) + }) + }) + describe('Defaults configuration', () => { test('should support default configurations', (done) => { expectResultsToContainReplacements(