Dotenv 18: CLI

Scott Motte ·

It's the biggest change to Dotenv in a decade, a CLI.

For years, dotenv has loaded your .env file from inside your application. Now it can load those variables before your command starts. Your app just reads its environment.

That makes the same .env file useful for scripts, tests, and other command-line tools, too. And it gives coding agents a simple way to run a command with the environment it needs.

Dotenv CLI in action

Usage

Install [email protected] or greater.

$ npm install dotenv --save

Create your .env file.

# .env
HELLO="World"

Create a simple app. There's no dotenv import needed here.

// index.js
console.log(`Hello ${process.env.HELLO}`)

Run it with the new CLI.

$ npx dotenv run -- node index.js
◇ injected env (1) from .env
Hello World

That's it. Dotenv loads .env, then starts Node with those variables available. The command after the -- can be any executable available on your PATH.

Choose your environment

By default, the CLI reads .env. To use another file, pass -f:

$ npx dotenv run -f .env.local -- node index.js

Variables already set in your environment take precedence. Add --override if you want the file's values to win instead.

Upgrading

The familiar require('dotenv').config() API is still here. If you used Node's -r dotenv/config preload, switch to the CLI command above. Version 18 also removes the legacy .env.vault format; use dotenvx for encrypted .env files.

There's an optional faster parser, too: add --fast to your CLI command to try it. See the release notes for the full changes.

A small command, a big step

Dotenv started with a simple idea: load your .env file and get to work. The CLI brings that same simplicity to any command - whether you run it yourself or an agent runs it for you. A small command, and a big next step for dotenv.