Overview
@std/env reads and writes the environment of the process itself. It covers the argument list the program was started with, the environment variables it inherited or set, the directory it is running in, and the call that ends it.
Nothing here reads a .env file and nothing is cached to disk. What you see
is what the process inherited at start, plus whatever you write with
Env.set after that.
The package has two faces:
Envis a class with static members. CallEnv.get,Env.set,Env.args,Env.cwd, andEnv.exitdirectly.RnxHostbacks the ambientrnxobject.rnx.versionis the compiler version,rnx.argsholds CLI arguments after the script path,rnx.cwd()reads the working directory, andrnx.exit(code)terminates. The stock prelude re-exportsRnxHost, so no import is needed forrnx.
A first program:
import { Env } from "@std/env";
Env.set("GREETING", "hi");
println!(Env.get("GREETING"));
println!(Env.args().len() > 0);
println!(Env.cwd().len() > 0);This prints hi, true, and true.
Values are strings
Every lookup returns a String. A name that is not set reads as an empty
string, and so does a value the host cannot decode as UTF-8. Nothing here
returns null.
Because of that, Env.get cannot tell an unset variable from one set to the
empty string. Compare against "" when you only need to know whether a
usable value is present. Process.env from @std/process returns String?
instead, so it can report the difference.
Env.set writes a value into this process. Setting an empty string sets the
name to empty; it does not remove it. Process.removeEnv is the call that
unsets a name.
Values do not reach child processes. A child started with Process.spawn or
Process.run starts from a cleared environment, so pass what the child
needs through SpawnOptions.env.
Guides
- Arguments covers
Env.argsand the ambientrnxobject. - Environment variables and the working directory
covers
Env.get,Env.set,Env.cwd, andEnv.exit.
Start with Getting started for a copyable program.