npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

add-env-var

v1.0.0

Published

A CLI tool to add environment variables to .env files

Downloads

16

Readme

add-env-var

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.

Features

  • 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.

Installation

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

Usage

Basic Usage

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.

Adding Multiple Variables

You can add multiple variables at once:

npx add-env-var VAR1 VAR2 VAR3

This will generate random secrets for VAR1, VAR2, and VAR3.

Setting Custom Values

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.

Specifying Secret Length

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.

Using a Different Environment File

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.

Using a Different Encoding

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.

Options

  • -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 are hex, base64, and base64url (default is hex).
  • -h, --help: Display help information.
  • -V, --version: Display the version number.

Examples

Example 1: Add a Variable with a Random Secret

npx add-env-var SESSION_SECRET

Adds SESSION_SECRET with a 32-byte random secret to .env.

Example 2: Add Multiple Variables with Custom Values

npx add-env-var API_KEY=abcdef123456 DB_HOST=localhost

Sets API_KEY to abcdef123456 and DB_HOST to localhost in .env.

Example 3: Specify Secret Length and File

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.

Example 4: Generate Secret with Base64 Encoding

npx add-env-var API_TOKEN -e base64

Adds API_TOKEN with a Base64-encoded secret to .env.

Example 5: Overwrite an Existing Variable

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.

Example 6: Using the Help Command

npx add-env-var --help

Displays all available options and usage information.