6.4.0 • Published 9 months ago

@hairy/lnv v6.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@hairy/lnv

npm version npm downloads bundle JSDocs License

Loading environment variables in Next.js and other frameworks can be quite cumbersome, and using dotenv or vault at runtime is also inconvenient. That's why my created this tool

I think it can solve some of the problems you encounter in next.js or dotenv or more. 🛠️

✅ Features

  • 🔄 Multi-mode support, allowing you to load .env|.env.prod|.env.local simultaneously
  • 🔐 Support for vault.dotenv remote environment variable loading and multi-environment switching
  • 🛡️ Direct writing to process.env, no local storage, more secure and reliable
  • 📁 Environment variables lookup, naturally supports monorepo architecture
  • 🏃‍♂️ Run any JS and scripts with environment variables

📦 Install

npm install @hairy/lnv
# or globally
npm install -g @hairy/lnv

🚀 Usage

Modify the scripts field in your package.json:

{
  "scripts": {
    "dev": "lnv staging -- next dev"
  }
}

This will launch next dev with the .env.staging environment variables. ✨

You can also include any run parameters, for example:

lnv prod -- next dev --turbopack

🌿 Default Environment Variables

You can include default environment variables using the --default|-d parameter:

# Will include .env.local and .env environment variables
lnv -d -- node index.js
# Equivalent to
lnv local env -- node index.js

🔒 Vault Environment Variables

Before loading variables from vault, you need to connect to the vault environment variable repository in your project:

# Create and connect to vault repository
npx dotenv-vault@latest new
# Connect to an existing vault repository
npx dotenv-vault@latest vlt_...

# And encrypt your environment variables:
npx dotenv-vault@latest pull
# or
npx dotenv-vault@latest build

Next, you need to create a .env.key or .env.keys file in your project root directory and write the DOTENV_KEY:

# .env.key
DOTENV_KEY = dotenv://...?environment=development

# or

# .env.keys
DOTENV_KEY_DEVELOPMENT = dotenv://...?environment=development
DOTENV_KEY_CI = dotenv://:...?environment=ci
DOTENV_KEY_STAGING = dotenv://:...?environment=staging
DOTENV_KEY_PRODUCTION = dotenv://:...?environment=production

Finally, you can use the lnv command to include vault environment variables:

# Load environment variables based on .env.vault and .env.key
lnv --vlt -- node index.js
# Load ci environment variables based on .env.vault and .env.keys
lnv --vlt ci -- node index.js

🚢 Vercel with Vault

If you need to deploy on Vercel, you need to add environment variables in your Vercel project:

npx vercel@latest env add DOTENV_KEY

lnv vault will automatically load environment variables based on .env.vault. 🎉

✍️ Manual with environment variables

You can manual load environment variables with the -v|--value parameter:

lnv -v KEY1=VALUE1 -v KEY2=VALUE2 -- node index.js

🔍️ Options

lnv <entry> [args]

args:
      --version       show version                                               [boolean]
  -v, --value         set environment variables                                  [array]
  -e, --entry         Explicit loading of entry, same as lnv <entry>             [string]
      --vault, --vlt  load environment variables from vault                      [string]
  -c, --cmd           load runtime environment and run any scripts               [string]
      --expose        expose environment variables                               [boolean]
  -d, --default       the default environment (env|env.local) be loaded          [boolean]
  -o, --overflow      deep find and merge environment variables                  [boolean]
  -h, --help          show help                                                  [boolean]

📄 License

MIT License © Hairyf

5.6.0

12 months ago

1.2.0

1 year ago

5.5.0

1 year ago

1.1.0

1 year ago

5.4.0

1 year ago

5.3.0

1 year ago

5.2.0

1 year ago

5.1.0

1 year ago

5.0.0

1 year ago

6.1.0

12 months ago

6.0.1

12 months ago

6.0.0

12 months ago

6.3.0

11 months ago

6.2.0

11 months ago

6.4.0

9 months ago

2.2.0

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

3.4.0

1 year ago

3.3.0

1 year ago

3.2.0

1 year ago

3.1.0

1 year ago

3.6.0

1 year ago

3.5.0

1 year ago

3.0.0

1 year ago

4.0.0

1 year ago

5.9.0

12 months ago

5.8.0

12 months ago

5.7.0

12 months ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago