mergeWithEnv

View as MarkdownOpen in Claude

Merge the loaded configuration with environment variables whose names start with a given prefix. Config file values take precedence over environment variables — env vars only fill in keys that are missing from the config.

Matching env var names are:

  1. Stripped of the prefix
  2. Lowercased
  3. Split on _ to produce a nested object path

For example, with prefix SWML_, SWML_SERVER_PORT=3000 produces { server: { port: "3000" } }.

All values inherited from env vars are written as strings (Node returns process.env values as strings). Apply substituteVars() to the result if you need type coercion.

Parameters

envPrefix
stringDefaults to 'SWML_'

Prefix for environment variables to consider.

Returns

Record<string, unknown> — the merged configuration (file + prefixed env vars) with ${VAR} substitution applied to file values.

Example

1import { ConfigLoader } from '@signalwire/sdk';
2
3// Given config.json: { "server": { "host": "0.0.0.0" } }
4// And env: SWML_SERVER_PORT=3000
5
6const loader = new ConfigLoader('config.json');
7const merged = loader.mergeWithEnv();
8console.log(merged);
9// { server: { host: "0.0.0.0", port: "3000" } }