Config library for nodejs, similar to node-config but strongly typed with validated type parsing. No need for Js.Nullable.t shims typical when just binding to js-libs.
Assuming You Write file MyConfig.re:
module C = BsNodeConfig.Config;
/*
* loadConfig with default options searches for .json and .yaml files from ./config/
* (loading may produce an error, therefore using getExn, which will throw if loading had errors)
*/
let config: C.t = C.loadConfig() |> C.getExn;
Usage in other files/modules:
module C = BsNodeConfig.Config;
let config = MyConfig.config;
let myServerHost: string = config |> C.key("server.host") |> C.parseStringExn;
let myList: list(string) = config |> C.key("listOfWords") |> C.parseList(C.parseString) |> C.getExn;
/* This alternative style works too: */
let myServerPort: int = C.(fetchExn("server.port", parseInt, Config.config));
Function C.loadConfig()
searches config values from following sources in following order:
- Loads config files:
- From directory
process.cwd() + "/config/"
OR if env variableNODE_CONFIG_DIR
is defined, that is used instead: -
default.{json,yaml}
is load if exists - Profile name defined by
NODE_CONFIG_ENV
if exists orNODE_ENV
if exists -> loads file${profileName}.{json,yaml}
if exists -
local.{json,yaml}
is load if exists
- From directory
- Loads Environment Variables
- Loads
CONFIG_JSON
contents - Reads file
custom-environment-variables.{json,yaml}
, which contains ENV variable name override definitions for various config keys (this is identical to: node-config), -> loads overrides from the defined env variables that are found.
- Loads
- Fallback to empty config if nothing from the above exists.