Core Systems
Configuration
MoroJS provides a powerful configuration system that supports config files, environment variables, TypeScript, and module-specific configuration with proper priority handling and type safety.
Overview
MoroJS provides a powerful configuration system that supports config files, environment variables, TypeScript, and module-specific configuration with proper priority handling and type safety.
Config Files
moro.config.js or moro.config.ts support
Environment Override
Environment variables take precedence
TypeScript Support
Full type safety and IDE autocompletion
Module Configuration
createModuleConfig() for module-specific settings
Configuration Priority
Loading Order (Highest to Lowest Priority)
- 1Environment Variables (highest priority)
- 2Configuration Files (moro.config.js or moro.config.ts)
- 3Schema Defaults (lowest priority)
1// Configuration loading priority:
2// 1. Environment Variables (highest)
3// 2. Configuration File (moro.config.js/ts)
4// 3. Schema Defaults (lowest)
5
6// Example: Database configuration
7// 1. DATABASE_URL env var = "postgresql://prod-server/db"
8// 2. moro.config.js = { database: { url: "postgresql://localhost/dev" } }
9// 3. Default = { database: { url: "sqlite://memory" } }
10//
11// Result: Uses "postgresql://prod-server/db" from env var
12
13import { createApp } from '@morojs/moro';
14
15const app = await createApp();
16// Configuration is automatically loaded and merged
17
18// Access configuration
19console.log(app.config.database.url); // "postgresql://prod-server/db"
20console.log(app.config.server.port); // From PORT env var or config fileBasic Configuration File
Create a moro.config.js file in your project root for zero-setup configuration. Perfect for JavaScript projects.
1// moro.config.js
2module.exports = {
3 server: {
4 port: 3000,
5 host: 'localhost',
6 environment: 'development'
7 },
8 database: {
9 type: 'postgresql',
10 host: 'localhost',
11 port: 5432,
12 username: 'myapp',
13 password: 'development-password',
14 database: 'myapp_dev'
15 },
16 security: {
17 cors: {
18 enabled: true,
19 origin: ['http://localhost:3000']
20 }
21 },
22 logging: {
23 level: 'info',
24 format: 'pretty'
25 }
26};TypeScript Configuration
Use moro.config.ts for full type safety, IDE autocompletion, and validation. Import the AppConfig type for complete type checking.
1// moro.config.ts
2import type { AppConfig, DeepPartial } from '@morojs/moro';
3
4const config: DeepPartial<AppConfig> = {
5 server: {
6 port: 3000,
7 environment: 'development'
8 },
9 database: {
10 url: process.env.DATABASE_URL,
11 pool: {
12 min: 2,
13 max: 10
14 }
15 },
16 logging: {
17 level: 'debug',
18 format: 'json'
19 },
20 auth: {
21 secret: process.env.AUTH_SECRET,
22 providers: ['github', 'google']
23 }
24};
25
26export default config;Module Configuration
Create module-specific configuration with Zod validation, default values, and environment variable override support. Perfect for reusable modules.
1import { createModuleConfig, z } from '@morojs/moro';
2
3// Email module configuration
4const emailConfig = createModuleConfig(
5 z.object({
6 apiKey: z.string(),
7 timeout: z.number().default(5000),
8 retries: z.number().default(3),
9 provider: z.enum(['sendgrid', 'mailgun']).default('sendgrid')
10 }),
11 {
12 // Default values
13 timeout: 3000,
14 provider: 'sendgrid'
15 },
16 'EMAIL_' // Environment prefix
17);
18
19// Usage in your module
20export function sendEmail(to: string, subject: string, body: string) {
21 const config = emailConfig.get();
22
23 // config.apiKey comes from EMAIL_API_KEY env var
24 // config.timeout comes from EMAIL_TIMEOUT env var or default
25 // config.provider comes from EMAIL_PROVIDER env var or default
26
27 return emailProvider.send({
28 apiKey: config.apiKey,
29 timeout: config.timeout,
30 // ...
31 });
32}Environment Variables
Environment variables have the highest priority and will override config file settings. Perfect for deployment-specific configuration and secrets.
# Environment variables (highest priority)
NODE_ENV=production
PORT=8080
DATABASE_URL=postgresql://user:pass@prod-db:5432/myapp
# Authentication
AUTH_SECRET=your-32-character-secret-minimum
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
# Module-specific (with EMAIL_ prefix)
EMAIL_API_KEY=your-sendgrid-api-key
EMAIL_TIMEOUT=10000
EMAIL_PROVIDER=sendgrid
# Logging
LOG_LEVEL=warn
LOG_FORMAT=jsonConfiguration Features
Core Features
- Automatic configuration discovery
- Environment variable override
- TypeScript support with full typing
- Zod schema validation
- Backward compatibility
Advanced Features
- Module-specific configuration
- Environment prefix support
- Automatic type coercion
- Configuration priority handling
- IDE autocompletion support