@bearz/env
Overview
The env provides a uniform way to work with environment variables and
the path variable across different runtimes such as bun, node, deno,
cloudflare and the browser and different operating system differences.
Cloudflare and the brower uses an in memory store.
Bash and Windows style variable expansion is included. The env
object provides additional methods to convert values to boolean,
int, number, json, etc.


Documentation
Documentation is available on jsr.io
A list of other modules can be found at github.com/bearz-io/js
Usage
import * as env from "@bearz/env";
console.log(env.get("USER") || env.get("USERNAME"));
env.set("MY_VAR", "test")
console.log(env.get("MY_VAR"))
console.log(env.expand("${MY_VAR}"));
console.log(env.expand("${NO_VALUE:-default}"));
console.log(env.get("NO_VALUE"));
env.expand("${NO_VALUE:=default}");
console.log(env.get("NO_VALUE"));
try {
env.expand("${REQUIRED_VAR:?Environment variable REQUIRED_VAR is missing}");
} catch(e) {
console.log(e.message);
}
console.log(env.proxy.MY_VAR);
env.proxy.MY_VAR = "test"
console.log(env.proxy.MY_VAR)
env.merge({
"VAR2": "VALUE",
"MY_VAR2": undefined
});
env.union({
"VAR2": undefined,
"NEW": "TEST",
"MY_VAR": "A"
})
env.set("MY_VAR", "test")
env.remove("MY_VAR");
env.appendPath("/opt/test/bin");
env.prependPath("/opt/test2/bin");
env.hasPath("/opt/test2/bin");
env.removePath("/opt/test2/bin");
env.replacePath("/opt/test/bin", "/opt/test2/bin")
console.log(env.splitPath());
console.log(env.getPath())
const path = env.getPath();
env.setPath(`${path}:/opt/test4/bin`)
Variables
proxy
- A proxy object that lets you interact with environment
variables the way you would with process.env
.
Functions
appendPath
- Appends a path to the PATH environment variable.
expand
- Expands string template with environment variables.
get
- Gets an environment variable.
getPath
- Gets the environment PATH value.
has
- Determines if an environment variable is set.
hasPath
- Determines if the PATH variable contains a path.
home
- Gets the home environment variable value.
hostname
- Gets the hostname environment variable value.
joinPath
- Joins paths into a single string.
merge
- Merges the values from an object into the environment variables.
prependPath
- Prepends a path to the PATH variable.
set
- Sets an environment variable.
setPath
- Sets the environment's PATH value.
remove
- Deletes an environment variable.
removePath
- Removes a path from the environment PATH value.
replacePath
- Replaces a path from the environment PATH value.
splitPath
- Splits the PATH variable into a string array.
shell
- Gets the shell environment variable value.
os
- Gets the os environment variable value.
toObject
- Clones and returns a copy of all environment values.
union
- Unions the vlaues from an object into the environment variables.
user
- Gets the user environment variable value.
License
MIT License