Command-line arguments are one source of configuration. @bomb.sh/router also binds JavaScript values, such as the contents of a configuration file, and environment variables onto the same route models. Every source goes through the same schema, so a value from the environment is validated exactly like a value from argv.
The process.argv property returns an array containing the command-line
arguments passed when the Node.js process was launched. The first element will
be
execPath
. See process.argv0 if access to the original value
of argv[0] is needed. The second element will be the path to the JavaScript
file being executed. The remaining elements will be any additional command-line
arguments.
For example, assuming the following script for process-args.js:
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
It is possible to modify this object, but such modifications will not be
reflected outside the Node.js process, or (unless explicitly requested)
to other Worker threads.
In other words, the following example would not work:
Assigning a property on process.env will implicitly convert the value
to a string. This behavior is deprecated. Future versions of Node.js may
throw an error when the value is not a string, number, or boolean.
import{ env }from'node:process';
env.test =null;
console.log(env.test);
// => 'null'
env.test =undefined;
console.log(env.test);
// => 'undefined'
Use delete to delete a property from process.env.
import{ env }from'node:process';
env.TEST=1;
delete env.TEST;
console.log(env.TEST);
// => undefined
On Windows operating systems, environment variables are case-insensitive.
import{ env }from'node:process';
env.TEST=1;
console.log(env.test);
// => 1
Unless explicitly specified when creating a Worker instance,
each Worker thread has its own copy of process.env, based on its
parent thread's process.env, or whatever was specified as the env option
to the Worker constructor. Changes to process.env will not be visible
across Worker threads, and only the main thread can make changes that
are visible to the operating system or to native add-ons. On Windows, a copy of process.env on a Worker instance operates in a case-sensitive manner
unlike the main thread.
A value source nests parameters under their route path. An environment variable joins the route path and the parameter name in upper snake case:
Route
Parameter
Value source
Environment variable
/pr/list
limit
{ pr: { list: { limit: 50 } } }
PR_LIST_LIMIT
/pr/create
title
{ pr: { create: { title: "Fix typo" } } }
PR_CREATE_TITLE
/pr/create
draft
{ pr: { create: { draft: true } } }
PR_CREATE_DRAFT
Parameters of the root route sit at the top level of a value source, and their environment variable is the bare name, such as VERBOSE for a root verbose toggle.
To read a different environment variable for one parameter, use env().
Command-line text and environment text are decoded before validation, as described in how values are decoded. So PR_LIST_LIMIT=40 reaches the schema as the number 40.
JavaScript values are used directly. A value source of { pr: { list: { limit: "50" } } } fails z.number(), because the string is not converted.
Toggles read from the environment accept exactly true or false.
Values attached with withValues() are addressed from the route they belong to, so /pr/list reads { limit: 50 }, not { pr: { list: { limit: 50 } } }. They take precedence over values passed to parse(). Environment sources attached with withEnvs() still use the full variable name, such as PR_LIST_LIMIT.
Deno asks for permission before a program reads environment variables. If you pass process.env to parse(), run your CLI with --allow-env (-E). Parsing argv alone needs no permission.