A powerful loader for your dotenv files, powered by dotenvx.
- Multi-environment support - Load different
.env
files for different environments - Encrypted envs - Securely store encrypted environment variables in your codebase
- Variable expansion - Reference and expand variables already on your machine
- Command substitution - Add the output of a command to your variables
- Configurable - Customize the plugin with various options
- Supports a wide variety of
.env
file types.env
,.env.local
,.env.staging
,.env.production
,.env.development
,.env.dev
,.env.beta
etc.
bun install -d bun-plugin-dotenvx
Next, ensure you are preloading the plugin in your bunfig.toml
:
preload = [
"./node_modules/bun-plugin-dotenvx/dist/index.js",
# ...
]
You may now use the decrypted .env
values in your project:
import env from './.env'
// Accessing environment variables
const apiKey = env.API_KEY
const databaseUrl = env.DATABASE_URL
console.log(`API Key: ${apiKey}`)
console.log(`Database URL: ${databaseUrl}`)
For the best development experience, we recommend installing the dotenvx VS Code extension. This extension provides syntax highlighting, encryption/decryption features, and other helpful tools directly in your editor.
# Install from VS Code marketplace
code --install-extension dotenv.dotenvx-vscode
Or search for "dotenvx" in the VS Code extensions marketplace.
For more information on dotenvx, visit dotenvx.com.
You can customize the plugin by creating your own instance with specific options:
import { plugin } from 'bun'
// plugin.ts
import { createDotenvxPlugin } from 'bun-plugin-dotenvx'
// Create a custom plugin instance
const dotenvxPlugin = createDotenvxPlugin({
path: ['.env.local', '.env'],
overload: true,
strict: true,
verbose: true,
})
// Register the plugin
await plugin(dotenvxPlugin)
Then in your bunfig.toml
:
preload = [
"./plugin.ts",
# ...
]
Load different .env
files for different environments:
import { plugin } from 'bun'
// development.ts
import { createDotenvxPlugin } from 'bun-plugin-dotenvx'
// production.ts
// import { createDotenvxPlugin } from 'bun-plugin-dotenvx'
await plugin(createDotenvxPlugin({
path: ['.env.development.local', '.env.development', '.env.local', '.env'],
}))
await plugin(createDotenvxPlugin({
path: ['.env.production.local', '.env.production', '.env'],
}))
To use encrypted environment variables:
- First, encrypt your
.env
file using dotenvx:
npx @dotenvx/dotenvx encrypt
- Then, use the plugin as normal, but make sure to set the
DOTENV_PRIVATE_KEY
environment variable:
DOTENV_PRIVATE_KEY="your-private-key" bun run your-script.ts
The plugin supports the following options:
Option | Type | Default | Description |
---|---|---|---|
path |
string | string[] |
['.env'] |
Path(s) to your env file(s) |
overload |
boolean |
false |
Override existing env variables |
strict |
boolean |
false |
Exit with code 1 if any errors are encountered |
ignore |
string[] |
undefined |
Ignore specific errors |
envKeysFile |
string |
'.env.keys' |
Path to your .env.keys file |
convention |
'nextjs' |
undefined |
Load a .env convention |
logLevel |
string |
'info' |
Set log level |
quiet |
boolean |
false |
Suppress all output (except errors) |
verbose |
boolean |
false |
Set log level to verbose |
debug |
boolean |
false |
Set log level to debug |
bun test
Please see our releases page for more information on what has changed recently.
Please review the Contributing Guide for details.
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
βSoftware that is free, but hopes for a postcard.β We love receiving postcards from around the world showing where bun-plugin-dotenvx
is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094 π
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
Many thanks to the following core technologies & people who have contributed to this package:
The MIT License (MIT). Please see LICENSE for more information.
Made with π