A CLI tool to easily add environment variables to .env
files. Generate random secrets, set custom values, and manage multiple environment files with ease. This will not add your .env
file to your .gitignore
so make sure you do that manually if relevant.
- Automatic .env File Handling: Automatically creates a
.env
file if one doesn't exist in your current directory. - Random Secret Generation: Generates cryptographically secure random secrets for your environment variables.
- Custom Variable Values: Set custom values for your environment variables directly from the command line.
- Overwrite Protection: Prompts for confirmation before overwriting existing variables.
- Multiple Variable Support: Add multiple environment variables in a single command.
- Custom Secret Length: Specify the length of the generated secrets.
- Custom Environment Files: Specify a different environment file instead of the default
.env
. - Custom Secret Encoding: Specify the encoding for the generated secret: hex (default), base64, or base64url.
You can use add-env-var
directly with npx
(no installation required):
npx add-env-var --help
Or install it globally:
npm install -g add-env-var
add-env-var --help
Add an environment variable with a generated random secret:
npx add-env-var YOUR_VARIABLE
This will add YOUR_VARIABLE
with a generated secret to your .env
file.
You can add multiple variables at once:
npx add-env-var VAR1 VAR2 VAR3
This will generate random secrets for VAR1
, VAR2
, and VAR3
.
Set custom values for your environment variables:
npx add-env-var API_KEY=12345 SECRET_KEY=mysecret
This will set API_KEY
to 12345
and SECRET_KEY
to mysecret
in your .env file.
Specify the length of the generated secrets (in bytes):
npx add-env-var TOKEN -l 64
This will generate a 64-byte (128 characters in hex) secret for TOKEN
.
Specify a different environment file instead of the default .env
:
npx add-env-var RANDOM_SECRET -f .env.local
This will add RANDOM_SECRET
to the .env.local
file.
Specify the encoding for the generated variable instead of the default hex
:
npx add-env-var RANDOM_VAR -e base64url
This will generate a URL-safe Base64-encoded secret for RANDOM_VAR
.
-l, --length <number>
: Length of the generated secret (default is 32 bytes).-f, --file <filepath>
: Specify a different environment file (default is.env
).-e, --encoding <type>
: Specify the encoding for the generated secret. Options arehex
,base64
, andbase64url
(default ishex
).-h, --help
: Display help information.-V, --version
: Display the version number.
npx add-env-var SESSION_SECRET
Adds SESSION_SECRET
with a 32-byte random secret to .env.
npx add-env-var API_KEY=abcdef123456 DB_HOST=localhost
Sets API_KEY
to abcdef123456
and DB_HOST
to localhost
in .env
.
npx add-env-var JWT_SECRET -l 64 -f .env.local
Generates a 64-byte secret for JWT_SECRET
and adds it to .env.local
.
npx add-env-var API_TOKEN -e base64
Adds API_TOKEN
with a Base64-encoded secret to .env
.
If a variable already exists, you'll be prompted:
Variable API_KEY already exists in .env. Overwrite? (y/N)
Enter y
to overwrite or n
to skip.
npx add-env-var --help
Displays all available options and usage information.