Skip to content

Migrate from dotenv

  • Validation: Catch misconfigurations early with schema-driven validation.
  • Security: Redact secrets and prevent accidental leaks.
  • Type-safety: Generate types automatically for your config.
  • External secrets: Load secrets from providers like 1Password, AWS, and more.

If you use dotenvx via the CLI, you can switch to varlock run:

Terminal window
# Before (dotenv CLI)
dotenvx run -- node app.js
# env specific
dotenvx run -f .env.staging -- node app.js
# install varlock
brew install dmno-dev/tap/varlock
# After (varlock CLI)
varlock run -- node app.js
# To specify an environment, set your env flag (see your .env.schema)
APP_ENV=staging varlock run -- node app.js

You can use multiple .env files (see Environments guide).


Initialize your project with varlock init to install varlock and generate a .env.schema from any existing .env files.

Terminal window
npx varlock init

Then to use varlock in your code, you can replace dotenv/config with varlock/auto-load:

index.js
// Before (dotenv)
import 'dotenv/config';
import 'varlock/auto-load';

Finally, you can remove dotenv from your dependencies:

Terminal window
npm uninstall dotenv

If dotenv is being used under the hood of one of your dependencies, you can use overrides to seamlessly swap in varlock instead.

See NPM overrides docs

package.json
{
"overrides": {
"dotenv": "varlock"
}
}