Configuration
The server ConfigBuilder accepts the following options. Only validate is required.
| Option | Default | Purpose |
|---|---|---|
validate |
— | Validates and types the merged config |
absoluteConfigFolderPath |
<process.cwd()>/config |
Where FileSource looks for files |
parser |
simple JSON parser | How file contents are parsed |
runtimeEnv |
process.env |
Where slots and env vars are read from |
slotPrefix |
"$" |
The prefix that marks a slot |
options.validate
Section titled “options.validate”The ConfigBuilder constructor requires a validate function. This function receives two arguments: the final merged configuration and a Zod instance (v4). It must return the validated configuration.
You can also use other validation libraries. Return the result of the schema validation from the validate function, and the config object will be typed accordingly:
options.absoluteConfigFolderPath
Section titled “options.absoluteConfigFolderPath”Default:
'<process.cwd()>/config'
By default, file-based sources added via addSource are loaded from a config folder in the project’s root. You can customize this path using the absoluteConfigFolderPath option:
In this example, the library will look for <root>/path/to/config-folder/base.json.
options.parser
Section titled “options.parser”Default: simple json parser
This option allows you to define a custom parser for non-JSON file types, such as .yaml, .jsonc, or .toml.
In this case, the library will look for <root>/config/base.yaml and load it.
options.runtimeEnv
Section titled “options.runtimeEnv”Default:
process.env
The object Layerfig reads environment values from. It is used in two places: to resolve slots such as ${PORT}, and as the input to EnvironmentVariableSource.
You normally leave it alone on the server. Override it when the environment does not live on process.env, or when you want to control it explicitly:
Passing an explicit object is also how you build a configuration in a test without touching the real environment — see the Testing guide.
options.slotPrefix
Section titled “options.slotPrefix”Default:
"$"
A string that identifies placeholders to be replaced with environment variables.
By default, Layerfig looks for placeholders prefixed with $. You can customize this prefix to avoid conflicts or to match a team’s convention.
For example, to use a double underscore (__) as the prefix:
Layerfig will now look for placeholders like __{PORT} in your configuration files.
Assuming the PORT environment variable is set, the resolved configuration will be: