Reference · General

env

signature
env(str, any=) -> any

What 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"))
output
true strnull fallback

Edge cases

let port = int(env("PORT") ?? "3000")print(port, type_of(port))print(env("MISSING_QUILLO_VAR", 42))print(len(env("PATH") ?? "") > 0)
output
3000 int42true
in a program
let 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.

this one fails
print(env(42))
output
line 1: `env` expects a str as argument 1, got int

Chaining

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))
output
trueNONE0.0.0.0 3000

See also

int, str, db.open, read_file

Thread
9Useful? Like it, or add to its thread below.

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