π§© hbh-ccl
A lightweight and flexible custom config loader for Node.js (ESM-only) projects.
hbh-ccl helps you load a configuration file from your project directory β either from the root or ./config/ folder β and gracefully fall back to a default config if none is provided.
β
Built for modern ESM syntax
β‘ Async and minimal
π§ Smart fallback + deep merging
β¨ Features
- β
Loads user-defined config from:
./<configName>or./config/<configName>
- π Falls back to a default config path (relative or absolute)
- π§ Supports both
export defaultand full module exports - β‘ Fully async and non-blocking
- ποΈ ESM-only (no
.json,.cjs, or CommonJS) - π§ͺ Clean and testable structure
π¦ Installation
Install via npm:
1npm install hbh-ccl
Or using yarn:
1yarn add hbh-ccl
π Usage
1οΈβ£ Create your config file
Create a config file in your project root or inside config/ folder (optional if you're using fallback only):
1// custom.config.js2export default {3 port: 3000,4 debug: true,5 database: {6 host: 'localhost',7 user: 'root'8 }9};
2οΈβ£ Load it in your app
1import { loadConfig } from 'hbh-ccl';3const config = await loadConfig({4 configName: 'custom.config.js', // optional5 defaultConfigPath: './default.config.js', // required6 verbose: true // optional7});9console.log('Loaded Config:', config);
π If the config isnβt found in
./or./config/, it will load the fallback fromdefaultConfigPath.
π Where It Looks for Config
loadConfig() checks for your config in the following order:
- π
./<configName>(project root) - π
./config/<configName>(config subfolder) - π§± Fallback:
defaultConfigPath
This ensures flexibility for both flat and organized project structures.
π API Reference
loadConfig(options) β Promise
Loads a config file, optionally merging it over a default one.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
configName |
string |
β | 'customName.config.js' |
Name of the config file to look for in cwd/ and cwd/config/ |
defaultConfigPath |
string |
β | None | Path to fallback config (absolute or relative to cwd) |
merge |
boolean |
β | true |
Whether to deep-merge user config over default |
verbose |
boolean |
β | false |
Log which config was loaded and from where |
cwd |
string |
β | process.cwd() |
Base path to resolve config paths |
Returns
Promise<Object>β The exported config object (fromexport defaultor full module)
π§ͺ Example Configs
β Default Config (used as fallback)
1// default.config.js2export default {3 port: 8080,4 debug: false5};
β User Config (with default export)
1// custom.config.js2export default {3 port: 4000,4 debug: true,5 featureFlag: true6};
β User Config (with named exports)
1// custom.config.js2export const port = 5000;3export const debug = true;
π§
hbh-cclsupports both default and named exports β whichever is present.
π Example Project Structure
my-app/
βββ custom.config.js # Optional: user config
βββ config/
β βββ custom.config.js # Also supported!
βββ default.config.js # Required fallback
βββ index.js
βββ package.json
β Important Notes
- β οΈ ESM-only: Config files must use
import/exportsyntax - β
.json,.cjs, orrequire()are not supported - π§± Node.js v14+ is recommended
- π Relative paths (like
./default.config.js) are resolved fromcwd
π‘ Advanced Notes
- π§ Uses
structuredClone()for deep cloning if available (Node.js 17+), with fallback toJSON.parse(JSON.stringify(...)) - π Configs are cached internally to prevent redundant imports (good for performance in CLI apps or scripts)
π Troubleshooting
β Error: Cannot find module
- Ensure your config file path is spelled correctly
- Use
verbose: trueto see what file is being resolved
β Unexpected token 'export'
- You're likely using CommonJS. Make sure your config files use
exportinstead ofmodule.exports
π Related Projects
| Project | Description |
|---|---|
| cosmiconfig | A powerful config loader that supports many file formats |
| conf | Simple config handling, great for CLIs and Electron apps |
π§©
hbh-cclis intentionally lightweight and minimal β no YAML, no JSON, no recursion, no surprises.
π License
This project is licensed under the ISC License.
βοΈ Author
Made with β€οΈ by HBH