Reference · General
env
env(str, any=) -> anyWhat it does
The value of an environment variable, as a string, or null when it is not set; with a
second argument, that default instead of null. The .env beside the program is read
into the environment when the program starts, and a variable the environment already
has wins over the file. A component cannot call env, except for a variable named
PUBLIC_..., whose value is written into the browser's bundle as it was when the
program loaded.
Examples
print(env("HOME") != null, type_of(env("HOME")))print(env("MISSING_QUILLO_VAR"), env("MISSING_QUILLO_VAR", "fallback"))true strnull fallbackEdge cases
- The value is always a string: a port or a count is converted with
int, and??gives the default when the variable is missing. - The default can be any value, not only a string:
env("X", 42)is42whenXis not set. - A variable that is set to the empty string is
"", notnull, so its default is not used. In the.env,#ends a bare value, soKEY= # set meis""whileKEY=#fffis#fff. - Only a
.envthat is not there is nothing. One that cannot be read, is not UTF-8, or is a link to nothing stops the start with exit 2, saying where and the ways out (quillo: cannot read .env: it is not UTF-8 text: byte 0xE9 at line 1, column 9 (save it as UTF-8); to start without it, remove it or give `--env-file /dev/null`). A line that is notKEY=value, or whose value holds a NUL byte, is skipped and said at the start; a quote never closed skips its own line only. - Under a policy whose
[env]givesnames(/etc/quillo/policy.toml,quillo run main.qwe --policy FILEorquillo.policybeside the program; see the options), a name not in the list reads as not set:null, or the default. Each policy file can only narrow the list.
let port = int(env("PORT") ?? "3000")print(port, type_of(port))print(env("MISSING_QUILLO_VAR", 42))print(len(env("PATH") ?? "") > 0)3000 int42truelet KEY = env("STRIPE_KEY") // on the server: fine at the top levelcomponent Map() { let token = env("PUBLIC_MAP_TOKEN") // in a component: only a PUBLIC_ name <div data-token={token}></div>}Errors
A name that is not a string is a runtime error, not an error value. A missing variable
is never an error: it is null or the default. Using env in a component, or a
top-level value made from it, is refused when the program loads.
print(env(42))line 1: `env` expects a str as argument 1, got intChaining
print(env("HOME") | contains("/"))print(env("MISSING_QUILLO_VAR") ?? "none" | upper)let [host, port] = split(env("LISTEN") ?? "0.0.0.0:3000", ":")print(host, int(port))trueNONE0.0.0.0 3000See also
Thread 0 posts
On env(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo