# fable-settings

> A simple, tolerant configuration chain.

Latest version **3.0.17** (published 2026-07-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install fable-settings
pnpm add fable-settings
yarn add fable-settings
bun add fable-settings
```

## Health

**Score 45/100 (D)** — status: active.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 3.0.17 |
| Published | 2026-07-16 |
| First published | 2015-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 636.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Steven Velozo |
| Maintainers | stevenvelozo, bigal |
| Keywords | configuration |

## Links

- npm: https://www.npmjs.com/package/fable-settings
- Repository: https://github.com/fable-retold/fable-settings
- Issues: https://github.com/fable-retold/fable-settings/issues
- npm.io page: https://npm.io/package/fable-settings

## Dependencies (2)

- [precedent](https://npm.io/package/precedent.md) ^1.0.17
- [fable-serviceproviderbase](https://npm.io/package/fable-serviceproviderbase.md) ^3.0.19

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 3.0.17 (latest) — 2026-07-16
- 3.0.16 — 2026-02-27
- 3.0.15 — 2026-02-18
- 3.0.14 — 2026-02-15
- 3.0.12 — 2024-09-25
- 3.0.11 — 2024-09-25
- 3.0.10 — 2024-06-08
- 3.0.9 — 2023-10-07
- 3.0.8 — 2023-07-13
- 3.0.6 — 2023-05-24
- 3.0.5 — 2023-05-10
- 3.0.4 — 2023-05-08
- 3.0.3 — 2023-02-28
- 3.0.1 — 2023-02-21
- 3.0.0 — 2023-02-21
- … 11 more at https://npm.io/package/fable-settings/versions

## README

# Fable Settings

> **[Read the Fable-Settings Documentation](https://fable-retold.github.io/fable-settings/)** - interactive docs with the full API reference.

A simple, tolerant configuration chain for Fable applications. Merges default settings, constructor overrides, JSON config files, and environment variables into a single settings object -- with deep merge, fill-without-overwrite, and `${ENV_VAR|default}` template syntax.

[fable-settings on npm](https://www.npmjs.com/package/fable-settings) | [MIT License](LICENSE)

---

## Features

- **Layered Configuration** - Merges defaults, constructor settings, default config file, and config file in order
- **Deep Merge** - Nested objects are merged recursively rather than replaced wholesale
- **Environment Variable Templating** - Use `${ENV_VAR}` or `${ENV_VAR|fallback}` syntax in any string setting value
- **Config File Loading** - Optionally load and merge settings from `DefaultConfigFile` and `ConfigFile` JSON paths
- **Fill Without Overwrite** - `fill()` adds missing keys from a source object without replacing existing values
- **Core Service** - Extends `fable-serviceproviderbase` CoreServiceProviderBase, available before Fable is fully initialized
- **Browser Support** - Ships with a browser shim that assigns `window.FableSettings` automatically

## Installation

```bash
npm install fable-settings
```

## Quick Start

```javascript
const libFableSettings = require('fable-settings');

let settings = new libFableSettings(
{
	Product: 'MyApp',
	ProductVersion: '2.0.0',
	Database:
	{
		Host: '${DB_HOST|localhost}',
		Port: 5432
	}
});

console.log(settings.settings.Product);         // 'MyApp'
console.log(settings.settings.Database.Host);    // value of $DB_HOST, or 'localhost'
```

### Legacy Factory

A `.new()` factory is available for backwards compatibility:

```javascript
let settings = require('fable-settings').new(
{
	Product: 'MyApp',
	ProductVersion: '1.0.0'
});
```

## Configuration Chain

Settings are resolved in the following order (later sources overwrite earlier ones):

1. **Built-in defaults** -- `Product`, `ProductVersion`, `ConfigFile`, `LogStreams`
2. **Constructor settings** -- the object passed to `new FableSettings(pSettings)`
3. **Default config file** -- if `DefaultConfigFile` is set, loaded and merged
4. **Config file** -- if `ConfigFile` is set, loaded and merged

Config file loading is fault-tolerant -- if a file cannot be loaded, a warning is logged and the previous settings are preserved.

### Default Settings

```json
{
	"Product": "ApplicationNameHere",
	"ProductVersion": "0.0.0",
	"ConfigFile": false,
	"LogStreams": [{ "level": "trace" }]
}
```

## Usage

### Merging Settings at Runtime

Use `merge()` to deep-merge new values into the current settings:

```javascript
let settings = new libFableSettings({ Product: 'MyApp' });

settings.merge({ Database: { Host: 'db.example.com', Port: 5432 } });
settings.merge({ Database: { Port: 3306 } });

// settings.settings.Database.Host === 'db.example.com'
// settings.settings.Database.Port === 3306
```

### Filling Gaps Without Overwriting

Use `fill()` to add missing keys without replacing existing values:

```javascript
let settings = new libFableSettings({ Product: 'MyApp', LogLevel: 'info' });

settings.fill({ Product: 'DefaultApp', CacheTTL: 3600, LogLevel: 'trace' });

// settings.settings.Product === 'MyApp'       (not overwritten)
// settings.settings.LogLevel === 'info'        (not overwritten)
// settings.settings.CacheTTL === 3600          (filled in)
```

### Environment Variable Templating

Any string value can reference environment variables with the `${VAR}` or `${VAR|default}` syntax. Variables are resolved recursively through nested objects:

```javascript
// Given: DB_HOST=prod-db.example.com in the environment

let settings = new libFableSettings(
{
	Database:
	{
		Host: '${DB_HOST|localhost}',
		Port: '${DB_PORT|5432}',
		Name: '${DB_NAME|myapp}'
	}
});

// settings.settings.Database.Host === 'prod-db.example.com'  (from env)
// settings.settings.Database.Port === '5432'                  (default)
// settings.settings.Database.Name === 'myapp'                 (default)
```

Disable environment variable templating by setting `NoEnvReplacement: true`:

```javascript
let settings = new libFableSettings(
{
	NoEnvReplacement: true,
	SomeValue: '${THIS_STAYS_LITERAL}'
});
```

### Loading Config Files

```javascript
let settings = new libFableSettings(
{
	Product: 'MyApp',
	DefaultConfigFile: '/etc/myapp/defaults.json',
	ConfigFile: '/etc/myapp/config.json'
});
```

## API

### Constructor

```javascript
new FableSettings(pSettings, pServiceHash)
```

| Parameter | Type | Description |
|-----------|------|-------------|
| `pSettings` | `Object` | Initial settings to merge over defaults |
| `pServiceHash` | `String` | Optional service hash identifier |

### Methods

| Method | Description |
|--------|-------------|
| `merge(pSettingsFrom, pSettingsTo)` | Deep-merge `pSettingsFrom` into `pSettingsTo` (defaults to `this.settings`). Resolves environment variable templates after merging. Returns the merged object. |
| `fill(pSettingsFrom)` | Deep-merge `this.settings` into a copy of `pSettingsFrom`, filling gaps without overwriting existing values. Returns `this.settings`. |
| `buildDefaultSettings()` | Returns a fresh copy of the built-in default settings object. |

### Properties

| Property | Type | Description |
|----------|------|-------------|
| `settings` | `Object` | The resolved settings object |
| `default` | `Object` | The built-in default settings |
| `base` | `Object` | Snapshot of settings after constructor merge, before config file loading |
| `settingsTemplateProcessor` | `Object` | The Precedent-based template processor for `${ENV}` resolution |

## Part of the Retold Framework

Fable Settings is a core service in the Fable ecosystem:

- [fable](https://github.com/fable-retold/fable) - Application services framework
- [fable-serviceproviderbase](https://github.com/fable-retold/fable-serviceproviderbase) - Service provider base class
- [precedent](https://github.com/fable-retold/precedent) - Template expression parser (powers environment variable substitution)
- [fable-log](https://github.com/fable-retold/fable-log) - Logging framework (uses `LogStreams` from settings)

## Testing

Run the test suite:

```bash
npm test
```

Run with coverage:

```bash
npm run coverage
```

## Related Packages

- [fable](https://github.com/fable-retold/fable) - Application services framework
- [fable-serviceproviderbase](https://github.com/fable-retold/fable-serviceproviderbase) - Service provider base class
- [precedent](https://github.com/fable-retold/precedent) - String template engine

## License

MIT

## Contributing

Pull requests are welcome. For details on our code of conduct, contribution process, and testing requirements, see the [Retold Contributing Guide](https://github.com/fable-retold/retold/blob/main/docs/contributing.md).

---
_Source: https://npm.io/package/fable-settings · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
