Features
ð Multi-Environment
Seamlessly switch between development, production, and custom environments using NODE_ENV
ð Secrets Management
Keep sensitive data separate from your codebase with dedicated secrets files
ð Deep Merge
Configurations are deeply merged, allowing granular overrides while preserving base settings
ð TypeScript Support
Full TypeScript definitions included for excellent IDE autocomplete and type safety
â Well Tested
96%+ code coverage with comprehensive test suite across multiple Node versions
ðŠķ Lightweight
Minimal dependencies, simple API, zero configuration to get started
Installation
npm install --save @simpleworkjs/conf
Quick Start
Create a conf directory in your project root:
your-project/
âââ node_modules/
âââ conf/
â âââ base.js # Required - base configuration
â âââ development.js # Optional - development environment config
â âââ production.js # Optional - production environment config
â âââ secrets.js # Optional - sensitive data (add to .gitignore!)
âââ package.json
Example: conf/base.js
module.exports = {
app: {
name: 'My Application',
port: 3000
},
database: {
host: 'localhost',
name: 'myapp'
}
};
Example: conf/production.js
module.exports = {
app: {
port: 8080
},
database: {
host: 'prod-db.example.com'
}
};
Example: conf/secrets.js
module.exports = {
database: {
password: 'super-secret-password',
username: 'dbuser'
},
apiKeys: {
stripe: 'sk_live_...'
}
};
Usage in your application:
const conf = require('@simpleworkjs/conf');
console.log(conf.app.name); // 'My Application'
console.log(conf.database.host); // 'localhost' in dev, 'prod-db.example.com' in prod
console.log(conf.database.password); // 'super-secret-password' (from secrets.js)
console.log(conf.environment); // 'development' or 'production'
Configuration Structure
Load Order
Configuration files are loaded and merged in the following order:
- base.js - Loaded first (required)
- <environment>.js - Loaded second, overrides base settings
- secrets.js - Loaded last, overrides all previous settings
Each subsequent file deeply merges with the previous configuration.
Environment Variables
| Variable | Description | Default |
|---|---|---|
NODE_ENV |
Sets the environment (determines which config file to load) | development |
CONF_DIR |
Override the configuration directory path | ./conf |
API Reference
The package exports a single configuration object that includes all merged settings plus:
environment- The current environment name (fromNODE_ENV)
TypeScript
Extend the Config interface for type safety:
declare module '@simpleworkjs/conf' {
interface Config {
app: {
name: string;
port: number;
};
database: {
host: string;
name: string;
password?: string;
};
}
}
import conf = require('@simpleworkjs/conf');
const port: number = conf.app.port; // Fully typed!
Examples
Using with Express
const conf = require('@simpleworkjs/conf');
const express = require('express');
const app = express();
app.listen(conf.app.port, () => {
console.log(`Server running on port ${conf.app.port}`);
});
Different Environments
# Development (default)
npm start
# Production
NODE_ENV=production npm start
# Custom environment
NODE_ENV=staging npm start # Loads conf/staging.js
Custom Config Directory
CONF_DIR=/path/to/config node app.js
Best Practices
- Always commit
base.jsand environment-specific files to version control - Never commit
secrets.js- add it to.gitignore - Use environment files for environment-specific URLs, ports, and settings
- Use secrets.js for API keys, passwords, tokens, and other sensitive data
- Keep base.js minimal - only include truly shared configuration
- Document your config - add comments explaining what each setting does
Contributing
Contributions are welcome! Please see our GitHub repository for guidelines.
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes and add tests
- Run tests:
npm test - Commit your changes
- Push to the branch
- Submit a pull request