diff --git a/CHANGELOG.md b/CHANGELOG.md index abd5cd41..bfd140c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. -## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.1.0...master) +## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.2.0...master) + +## [16.2.0](https://github.com/motdotla/dotenv/compare/v16.1.4...v16.2.0) (2023-06-15) ### Added +- Optionally write to your own target object rather than `process.env`. Defaults to `process.env`. [#753](https://github.com/motdotla/dotenv/pull/753) - Add import type URL to types file [#751](https://github.com/motdotla/dotenv/pull/751) ## [16.1.4](https://github.com/motdotla/dotenv/compare/v16.1.3...v16.1.4) (2023-06-04) diff --git a/README.md b/README.md index 13f7e05a..f9a60045 100644 --- a/README.md +++ b/README.md @@ -283,7 +283,7 @@ You can additionally, pass options to `config`. #### Options -##### Path +##### path Default: `path.resolve(process.cwd(), '.env')` @@ -293,7 +293,7 @@ Specify a custom path if your file containing environment variables is located e require('dotenv').config({ path: '/custom/path/to/.env' }) ``` -##### Encoding +##### encoding Default: `utf8` @@ -303,7 +303,7 @@ Specify the encoding of your file containing environment variables. require('dotenv').config({ encoding: 'latin1' }) ``` -##### Debug +##### debug Default: `false` @@ -313,7 +313,7 @@ Turn on logging to help debug why certain keys or values are not being set as yo require('dotenv').config({ debug: process.env.DEBUG }) ``` -##### Override +##### override Default: `false` @@ -323,6 +323,20 @@ Override any environment variables that have already been set on your machine wi require('dotenv').config({ override: true }) ``` +##### processEnv + +Default: `process.env` + +Specify an object to write your secrets to. Defaults to `process.env` environment variables. + +```js +const myObject = {} +require('dotenv').config({ processEnv: myObject }) + +console.log(myObject) // values from .env or .env.vault live here now. +console.log(process.env) // this was not changed or written to +``` + ### Parse The engine which parses the contents of your file containing environment @@ -338,7 +352,7 @@ console.log(typeof config, config) // object { BASIC : 'basic' } #### Options -##### Debug +##### debug Default: `false` @@ -379,7 +393,7 @@ dotenv.populate(target, parsed, { override: true, debug: true }) console.log(target) // { HELLO: 'universe' } ``` -#### Options +#### options ##### Debug @@ -387,12 +401,28 @@ Default: `false` Turn on logging to help debug why certain keys or values are not being populated as you expect. -##### Override +##### override Default: `false` Override any environment variables that have already been set. +### Decrypt + +The engine which decrypts the ciphertext contents of your .env.vault file is available for use. It accepts a ciphertext and a decryption key. It uses AES-256-GCM encryption. + +For example, decrypting a simple ciphertext: + +```js +const dotenv = require('dotenv') +const ciphertext = 's7NYXa809k/bVSPwIAmJhPJmEGTtU0hG58hOZy7I0ix6y5HP8LsHBsZCYC/gw5DDFy5DgOcyd18R' +const decryptionKey = 'ddcaa26504cd70a6fef9801901c3981538563a1767c297cb8416e8a38c62fe00' + +const decrypted = dotenv.decrypt(ciphertext, decryptionKey) + +console.log(decrypted) // # development@v6\nALPHA="zeta" +``` + ## ❓ FAQ ### Why is the `.env` file not loading my environment variables successfully? diff --git a/lib/main.js b/lib/main.js index 367d02e7..69cb939f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -162,7 +162,12 @@ function _configVault (options) { const parsed = DotenvModule._parseVault(options) - DotenvModule.populate(process.env, parsed, options) + let processEnv = process.env + if (options && options.processEnv != null) { + processEnv = options.processEnv + } + + DotenvModule.populate(processEnv, parsed, options) return { parsed } } @@ -185,7 +190,12 @@ function configDotenv (options) { // Specifying an encoding returns a string instead of a buffer const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding })) - DotenvModule.populate(process.env, parsed, options) + let processEnv = process.env + if (options && options.processEnv != null) { + processEnv = options.processEnv + } + + DotenvModule.populate(processEnv, parsed, options) return { parsed } } catch (e) { diff --git a/tests/test-config-vault.js b/tests/test-config-vault.js index 16d3e774..d8cd313c 100644 --- a/tests/test-config-vault.js +++ b/tests/test-config-vault.js @@ -194,6 +194,21 @@ t.test('does write over keys already in process.env if override turned on', ct = ct.equal(process.env.ALPHA, 'zeta') }) +t.test('can write to a different object rather than process.env', ct => { + ct.plan(3) + + process.env.ALPHA = 'other' // reset process.env + + logStub = sinon.stub(console, 'log') + + const myObject = {} + + const result = dotenv.config({ path: testPath, processEnv: myObject }) + ct.equal(result.parsed.ALPHA, 'zeta') + ct.equal(process.env.ALPHA, 'other') + ct.equal(myObject.ALPHA, 'zeta') +}) + t.test('logs when debug and override are turned on', ct => { ct.plan(1) diff --git a/tests/test-config.js b/tests/test-config.js index ea390a5e..65ef1433 100644 --- a/tests/test-config.js +++ b/tests/test-config.js @@ -135,6 +135,20 @@ t.test( } ) +t.test('can write to a different object rather than process.env', ct => { + ct.plan(3) + + process.env.test = 'other' // reset process.env + + const myObject = {} + const env = dotenv.config({ processEnv: myObject }) + + ct.equal(env.parsed && env.parsed.test, mockParseResponse.test) + console.log('logging', process.env.test) + ct.equal(process.env.test, 'other') + ct.equal(myObject.test, mockParseResponse.test) +}) + t.test('returns parsed object', ct => { ct.plan(2)