flowchart TD
A[Environment string] --> B[Split by separator]
B --> C[Trim whitespace]
C --> D[Remove empty items]
D --> E[Run the parser on each remaining item]
E --> F[Typed list]
The parser must throw when it finds an invalid item. The library does not automatically treat a returned NaN as an error.
const ports = env.list("ALLOWED_PORTS", value => {
if (!/^\d+$/.test(value)) thrownewError("Expected decimal digits");
const port = Number(value);
if (!Number.isSafeInteger(port) || port < 1 || port > 65535) {
thrownewError("Port out of range");
}
return port;
});
Regular expressions
The environment value is the pattern itself, not a JavaScript regex literal. ^[a-z]+$ is correct; the text /^[a-z]+$/i does not enable the i flag. There is no separate flags parameter for patterns read from the environment; a default RegExp object may have flags. An empty (or whitespace-only) value throws instead of producing a pattern that matches everything.
Values and Lists
Method overview
string(key, defaultValue?, trim = true)stringint(key, defaultValue?, radix = 10)number0xprefix is allowed for radix 16.float(key, defaultValue?)numberboolean(key, defaultValue?)booleantrue/false,yes/no,1/0; case-insensitive.oneOf(key, values, defaultValue?)valuesmay be anas constarray.regex(key, defaultValue?)RegExpnew RegExpconstructor; an empty pattern is rejected.list(key, parser, defaultValue?, separator = ",")T[]const name = env.string("APP_NAME", "My app"); const raw = env.string("RAW_TEXT", undefined, false); const port = env.int("PORT", 3000); const ratio = env.float("SAMPLING_RATIO", 0.5); const debug = env.boolean("DEBUG", false); const mode = env.oneOf("APP_ENV", ["dev", "prod", "test"], "prod"); const pattern = env.regex("NAME_PATTERN", /^[a-z]+$/i);Lists
// ALLOWED_HOSTS= example.com, api.example.com, , const hosts = env.list("ALLOWED_HOSTS", value => value); // ["example.com", "api.example.com"] const labels = env.list("LABELS", value => value, [], ";");flowchart TD A[Environment string] --> B[Split by separator] B --> C[Trim whitespace] C --> D[Remove empty items] D --> E[Run the parser on each remaining item] E --> F[Typed list]The parser must throw when it finds an invalid item. The library does not automatically treat a returned
NaNas an error.const ports = env.list("ALLOWED_PORTS", value => { if (!/^\d+$/.test(value)) throw new Error("Expected decimal digits"); const port = Number(value); if (!Number.isSafeInteger(port) || port < 1 || port > 65535) { throw new Error("Port out of range"); } return port; });Regular expressions
The environment value is the pattern itself, not a JavaScript regex literal.
^[a-z]+$is correct; the text/^[a-z]+$/idoes not enable theiflag. There is no separate flags parameter for patterns read from the environment; a defaultRegExpobject may have flags. An empty (or whitespace-only) value throws instead of producing a pattern that matches everything.Continue with URLs and Mappings.