Keep the private key separate from the encrypted .env file—for example, in your cloud platform's secrets manager. Read the dotenvx whitepaper for more details.
Custom Process Environment
Use processEnv to write expanded values to an object other than process.env:
Use $(command) in your .env file. dotenv-expand runs the command and replaces the expression with its output. See the Command Substitution example under Advanced.
How does encryption work?
Use dotenvx to encrypt your .env file. dotenv-expand automatically decrypts encrypted: values when DOTENV_PRIVATE_KEY is set.
Keep the private key separate from the encrypted .env file—for example, in your cloud platform's secrets manager. Read the dotenvx whitepaper for more details.
Is it safe to commit an encrypted `.env` file?
Yes, as long as the corresponding DOTENV_PRIVATE_KEY is stored separately and never committed with it. Use dotenvx to create and manage the encrypted file.
How can I avoid expanding pre-existing envs?
As of v12.0.0 dotenv-expand no longer expands process.env.
If you need this ability, use dotenvx and ship an encrypted .env file with your code, allowing safe expansion at runtime.
How can I override an existing environment variable?
Use dotenvx, as dotenv-expand does not support this.
dotenv-expand is a separate module (without knowledge of the loading of process.env and the .env file) and so cannot reliably know what to override.
Docs
Expand
expand expands, evaluates, and decrypts values in a dotenv result: