Comparing version 16.3.2 to 16.4.0
@@ -5,4 +5,9 @@ # Changelog | ||
## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.3.1...master) | ||
## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.4.0...master) | ||
## [16.4.0](https://github.com/motdotla/dotenv/compare/v16.3.2...v16.4.0) (2024-01-23) | ||
- Add `error.code` to error messages around `.env.vault` decryption handling [#795](https://github.com/motdotla/dotenv/pull/795) | ||
- Add ability to find `.env.vault` file when filename(s) passed as an array [#784](https://github.com/motdotla/dotenv/pull/784) | ||
## [16.3.2](https://github.com/motdotla/dotenv/compare/v16.3.1...v16.3.2) (2024-01-18) | ||
@@ -9,0 +14,0 @@ |
// TypeScript Version: 3.0 | ||
/// <reference types="node" /> | ||
import type { URL } from 'node:url'; | ||
import type { URL } from 'url'; | ||
@@ -26,6 +26,8 @@ export interface DotenvParseOutput { | ||
* Specify a custom path if your file containing environment variables is located elsewhere. | ||
* Can also be an array of strings, specifying multiple paths. | ||
* | ||
* example: `require('dotenv').config({ path: '/custom/path/to/.env' })` | ||
* example: `require('dotenv').config({ path: ['/path/to/first.env', '/path/to/second.env'] })` | ||
*/ | ||
path?: string | URL; | ||
path?: string | string[] | URL; | ||
@@ -32,0 +34,0 @@ /** |
@@ -56,3 +56,5 @@ const fs = require('fs') | ||
if (!result.parsed) { | ||
throw new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`) | ||
const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`) | ||
err.code = 'MISSING_DATA' | ||
throw err | ||
} | ||
@@ -125,3 +127,5 @@ | ||
if (error.code === 'ERR_INVALID_URL') { | ||
throw new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=development') | ||
const err = new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=development') | ||
err.code = 'INVALID_DOTENV_KEY' | ||
throw err | ||
} | ||
@@ -135,3 +139,5 @@ | ||
if (!key) { | ||
throw new Error('INVALID_DOTENV_KEY: Missing key part') | ||
const err = new Error('INVALID_DOTENV_KEY: Missing key part') | ||
err.code = 'INVALID_DOTENV_KEY' | ||
throw err | ||
} | ||
@@ -142,3 +148,5 @@ | ||
if (!environment) { | ||
throw new Error('INVALID_DOTENV_KEY: Missing environment part') | ||
const err = new Error('INVALID_DOTENV_KEY: Missing environment part') | ||
err.code = 'INVALID_DOTENV_KEY' | ||
throw err | ||
} | ||
@@ -150,3 +158,5 @@ | ||
if (!ciphertext) { | ||
throw new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`) | ||
const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`) | ||
err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT' | ||
throw err | ||
} | ||
@@ -158,10 +168,23 @@ | ||
function _vaultPath (options) { | ||
let dotenvPath = path.resolve(process.cwd(), '.env') | ||
let possibleVaultPath = null | ||
if (options && options.path && options.path.length > 0) { | ||
dotenvPath = options.path | ||
if (Array.isArray(options.path)) { | ||
for (const filepath of options.path) { | ||
if (fs.existsSync(filepath)) { | ||
possibleVaultPath = filepath.endsWith('.vault') ? filepath : `${filepath}.vault` | ||
} | ||
} | ||
} else { | ||
possibleVaultPath = options.path.endsWith('.vault') ? options.path : `${options.path}.vault` | ||
} | ||
} else { | ||
possibleVaultPath = path.resolve(process.cwd(), '.env.vault') | ||
} | ||
// Locate .env.vault | ||
return dotenvPath.endsWith('.vault') ? dotenvPath : `${dotenvPath}.vault` | ||
if (fs.existsSync(possibleVaultPath)) { | ||
return possibleVaultPath | ||
} | ||
return null | ||
} | ||
@@ -229,4 +252,2 @@ | ||
function config (options) { | ||
const vaultPath = _vaultPath(options) | ||
// fallback to original dotenv if DOTENV_KEY is not set | ||
@@ -237,4 +258,6 @@ if (_dotenvKey(options).length === 0) { | ||
const vaultPath = _vaultPath(options) | ||
// dotenvKey exists but .env.vault file does not exist | ||
if (!fs.existsSync(vaultPath)) { | ||
if (!vaultPath) { | ||
_warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`) | ||
@@ -266,10 +289,10 @@ | ||
if (isRange || invalidKeyLength) { | ||
const msg = 'INVALID_DOTENV_KEY: It must be 64 characters long (or more)' | ||
throw new Error(msg) | ||
const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)') | ||
err.code = 'INVALID_DOTENV_KEY' | ||
throw err | ||
} else if (decryptionFailed) { | ||
const msg = 'DECRYPTION_FAILED: Please check your DOTENV_KEY' | ||
throw new Error(msg) | ||
const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY') | ||
err.code = 'DECRYPTION_FAILED' | ||
throw err | ||
} else { | ||
console.error('Error: ', error.code) | ||
console.error('Error: ', error.message) | ||
throw error | ||
@@ -286,3 +309,5 @@ } | ||
if (typeof parsed !== 'object') { | ||
throw new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate') | ||
const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate') | ||
err.code = 'OBJECT_REQUIRED' | ||
throw err | ||
} | ||
@@ -289,0 +314,0 @@ |
{ | ||
"name": "dotenv", | ||
"version": "16.3.2", | ||
"version": "16.4.0", | ||
"description": "Loads environment variables from .env file", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
@@ -193,2 +193,32 @@ <div align="center"> | ||
Use [dotenvx](https://github.com/dotenvx/dotenvx) or [dotenv-vault](https://github.com/dotenv-org/dotenv-vault). | ||
### dotenvx | ||
Run any environment locally. Create a `.env.ENVIRONMENT` file and use `--env-file` to load it. It's straightforward, yet flexible. | ||
```bash | ||
$ echo "HELLO=production" > .env.production | ||
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js | ||
$ dotenvx run --env-file=.env.production -- node index.js | ||
Hello production | ||
> ^^ | ||
``` | ||
or with multiple .env files | ||
```bash | ||
$ echo "HELLO=local" > .env.local | ||
$ echo "HELLO=World" > .env | ||
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js | ||
$ dotenvx run --env-file=.env.local --env-file=.env -- node index.js | ||
Hello local | ||
``` | ||
[more environment examples](https://dotenvx.com/docs/quickstart/environments) | ||
### dotenv-vault | ||
Edit your production environment variables. | ||
@@ -300,2 +330,8 @@ | ||
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 loaded in order. The first value set for a variable will win. | ||
```js | ||
require('dotenv').config({ path: ['.env.local', '.env'] }) | ||
``` | ||
##### encoding | ||
@@ -302,0 +338,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74347
452
670