env
Environment variables available to this run.
Provides read-only access to named values supplied for the current run — your saved secrets plus any variables set for this specific run. Values are strings; names are case-sensitive. The environment is resolved before the script starts, so reads are synchronous.
Usage Examples#
Reading environment variables#
import env from "env";
try {
const token = env.get("API_TOKEN");
console.log("Token:", token);
} catch (err) {
if (err.code === env.ErrorCodes.NotFound) {
console.log("API_TOKEN is not set");
}
}
Listing available variables#
import env from "env";
for (const name of env.names()) {
console.log(name);
}
Constants
MAX_NAME_LENGTH
Maximum variable name length accepted by env.get().
Enums
ErrorCodes
Error codes for env operations.
| Member | Description |
|---|---|
NotFound |
No variable with the given name exists. |
InvalidName |
The given name is not a valid variable name. |
Unavailable |
The environment is not available in this run. |
Functions
get
get(name)
Get the value of an environment variable by name.
Parameters
| Name | Type | Description |
|---|---|---|
name |
string |
The variable name |
Returns
string — The value
Throws
TypeError— If name is not a stringError— With code `EINVALNAME` if the name is invalid, `ENOTFOUND` if no variable with that name exists, or `ENVUNAVAILABLE` if the environment is not available in this run
Example
import env from "env";
const apiKey = env.get("API_KEY");
const res = await fetch(`https://api.example.com`, {
headers: { Authorization: `Bearer ${apiKey}` },
});
names
names()
List the names of all environment variables available to this run. Values are never included.
Returns
Array.<string> — Sorted array of names
Throws
Error— With code `ENVUNAVAILABLE` if the environment is not available in this run
Example
import env from "env";
const names = env.names();
console.log(names); // ["API_KEY", "BASE_URL"]