How to Encrypt .env Files: dotenvx, SOPS and goraz Compared
A plain .env file is one mistaken commit away from a leak. Three practical ways to encrypt it, with the exact commands, and how to choose between dotenvx, SOPS with age and goraz.
- encrypt env file
- dotenvx
- sops age
- secrets management
- goraz

Most projects keep their database password and API keys in a .env file: plain text, sitting next to the code. A .gitignore line is the only thing stopping it from reaching GitHub, and that is not much. AI coding agents have made it worse: they have leaked thousands of real secrets by copying them into files that get committed.
Encrypting the file means a leaked copy is useless without the key. Here are three practical ways to do it.
Option 1: dotenvx
dotenvx is from the author of the original dotenv package. It encrypts the values inside your .env file and keeps the private key in a separate .env.keys file.
npm install @dotenvx/dotenvx --save
npx dotenvx encrypt
Now .env holds encrypted values and can be committed. Keep .env.keys out of git and give the key to your servers as the DOTENV_PRIVATE_KEY environment variable. Run your app with:
npx dotenvx run -- node index.js
Good for: Node.js projects that already use .env, and teams that want the smallest change.
Option 2: SOPS with age
SOPS encrypts structured files (YAML, JSON, .env) and works with many key types. age is the simplest one.
age-keygen -o key.txt
sops encrypt --age <your-age-public-key> --input-type dotenv --output-type dotenv .env > .env.enc
Commit .env.enc, keep key.txt safe, and run your app with the decrypted values:
SOPS_AGE_KEY_FILE=key.txt sops exec-env .env.enc 'node index.js'
Good for: teams with several languages and environments, or anyone already using cloud KMS keys (SOPS supports AWS, GCP and Azure KMS as well as age).
Option 3: goraz
goraz is an open-source secrets tool I built. Instead of encrypting the .env file itself, it moves the secrets into an encrypted vault and leaves you a file of references that is safe to commit.
goraz import .env --prefix myapp
Then create .env.goraz, which contains no secrets:
DATABASE_URL=goraz://myapp/DATABASE_URL
STRIPE_KEY=goraz://myapp/STRIPE_KEY
And run your program:
goraz run --env-file .env.goraz -- npm start
For Go services, goraz can also give each service its own encrypted file holding only the secrets it needs, read directly in code instead of through environment variables. The goraz-example repo shows a full service doing this. goraz is a young project and has not had an independent security audit yet.
Good for: Go services, and teams that want per-service secret files, rotation and an audit log.
Which one should you use?
| dotenvx | SOPS + age | goraz | |
|---|---|---|---|
| Change to your setup | Smallest | Medium | Medium |
| Works with any language | Yes | Yes | Yes (goraz run) |
| Cloud KMS keys | No | Yes | No |
| Per-service secret files | No | Manual | Yes |
| Best fit | Node.js apps | Mixed stacks, cloud KMS | Go services |
Whatever you choose
- If your
.envwas ever committed or shared, rotate the secrets. Encrypting the file now does not un-leak old values in git history. - Keep the key separate from the file. An encrypted file next to its key protects nothing.
- Do not paste secrets into AI agents or commands they can see. Reference environment variables instead.
If you want help moving a project’s secrets out of plain text, or a security review before launch, send me a message.