Skip to content

bunfigSmart configuration for modern Bun applications

TypeScript-first configuration loader with automatic environment variable detection, validation, and zero dependencies.

Quick Example

ts
// app.config.ts
export default {
  server: {
    port: 3000,
    host: 'localhost'
  },
  database: {
    url: 'postgresql://localhost:5432/myapp',
    pool: 10
  }
}
ts
// app.ts
import { config } from 'bunfig'

const { server, database } = await config({ name: 'app' })

console.log(`Server starting on ${server.host}:${server.port}`)
// Environment variables automatically override:
// APP_SERVER_PORT=8080 → server.port becomes 8080

Why Choose bunfig?

Featurebunfigc12dotenvnode-configcosmiconfigrc
TypeScript First⚠️⚠️
Zero Dependencies
Auto Env Vars⚠️⚠️⚠️
Built-in Validation
Multiple Sources
Home Directory (~/.config)⚠️⚠️
Hot Reload⚠️
Error Recovery⚠️
Performance Caching⚠️
Smart Type Conversion⚠️⚠️
Package.json Support
Bun Optimized⚠️⚠️⚠️⚠️⚠️

What Makes bunfig Special?

🧠 Intelligent by Design

bunfig doesn't just load configuration - it understands it. With smart naming conventions, automatic type conversion, and intelligent file discovery, it feels like magic:

bash
# Set an environment variable
export APP_DATABASE_POOL_SIZE=20

# bunfig automatically maps it to
config.database.pool.size = 20 // (as a number!)

🔄 Development to Production

Seamlessly transition from development to production with the same configuration API:

ts
// Development: loads from app.config.ts
const config = await loadConfig({ name: 'app' })

// Production: automatically uses environment variables
// APP_DATABASE_URL, APP_SERVER_PORT, etc.

🛡️ Enterprise Grade

Built for production with comprehensive error handling, validation, and monitoring:

ts
const config = await loadConfig({
  name: 'app',
  schema: productionSchema,
  fallback: safeDefaults,
  validate: customValidation
})

Real-World Examples

Web Server Configuration

ts
// server.config.ts
export default {
  http: { port: 3000, host: 'localhost' },
  cors: { enabled: true, origins: ['http://localhost:3000'] },
  rateLimit: { enabled: true, maxRequests: 100 }
}

// Automatically uses environment variables:
// SERVER_HTTP_PORT=8080
// SERVER_CORS_ORIGINS=https://myapp.com,https://api.myapp.com

Database Configuration with Validation

ts
const dbConfig = await config({
  name: 'database',
  schema: {
    type: 'object',
    properties: {
      url: { type: 'string', pattern: '^postgresql://' },
      pool: { type: 'number', minimum: 1, maximum: 100 }
    },
    required: ['url']
  }
})

Multi-Environment Setup

ts
// config/development.config.ts
export default {
  database: { url: 'postgresql://localhost:5432/dev' },
  logging: { level: 'debug' }
}

// config/production.config.ts
export default {
  database: { url: process.env.DATABASE_URL },
  logging: { level: 'error' }
}

// Load environment-specific config
const env = process.env.NODE_ENV || 'development'
const config = await loadConfig({ name: env, cwd: './config' })

Get Started in Minutes

  1. Install bunfig

    bash
    bun add bunfig
  2. Create a config file

    ts
    // app.config.ts
    export default {
      port: 3000,
      host: 'localhost'
    }
  3. Load and use

    ts
    import { config } from 'bunfig'
    const { port, host } = await config({ name: 'app' })

Meet The Team

Chris Breuer's avatar
Chris Breuer
Open sourceror.
Core Stacks team.
Working at Stacks.js
Glenn's avatar
Glenn
Open sourceror.
Core Stacks team.
Working at Stacks.js
Mike's avatar
Mike
Open sourceror.
Core Stacks team.
Working at Stacks.js
Zoltan's avatar
Zoltan
Open sourceror.
Core Stacks team.

Stacks Sponsors

Click here to become a sponsor.

Contributors

chrisbbreuer's avatarcab-mikee's avatar

Released under the MIT License.