Basic Usage Example
This example demonstrates the simplest way to use bun-plugin-dotenvx in your Bun project.
Setup
First, install the plugin:
bash
bun install -d bun-plugin-dotenvxThen, add it to your bunfig.toml:
toml
preload = [ "./node_modules/bun-plugin-dotenvx/dist/index.js" ]Create a .env File
Create a .env file in your project root:
ini
# .env
API_KEY=your_secret_api_key
DATABASE_URL=postgres://user:password@localhost:5432/mydb
NODE_ENV=developmentUse in Your Code
Now you can import your .env file directly in your code:
ts
// index.ts
import env from './.env'
console.log(`API Key: ${env.API_KEY}`)
console.log(`Database URL: ${env.DATABASE_URL}`)
console.log(`Node Environment: ${env.NODE_ENV}`)Run Your Application
Run your application with Bun:
bash
bun run index.tsYou should see your environment variables printed to the console:
API Key: your_secret_api_key
Database URL: postgres://user:password@localhost:5432/mydb
Node Environment: developmentHow It Works
The plugin automatically loads your .env file and makes its contents available as a module that you can import. This approach provides several benefits:
- Type Safety: Your IDE can provide autocomplete for your environment variables
- Direct Imports: No need to use
process.envor other global objects - Simplicity: No additional configuration required
For more advanced usage, check out the Multi-Environment Example and Encrypted Envs Example.