Skip to main content
Home

@gnome/env@0.4.0
Built and signed on GitHub Actions

a cross runtime module for managing environment variables which includes bash like variable expansion and manipulating the path variable.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
70%
Published
a year ago (0.4.0)

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 workers.

Variable expansion is included.

Basic Usage

import { env, HOME, USER } from "@gnome/env";

// get values
console.log(env.get("USER") || env.get("USERNAME"));
console.log(env.get(USER)); // gets the os specific name for user
console.log(env.get(HOME)); // gets the os specific name for home

// set variable
env.set("MY_VAR", "test")
console.log(env.get("MY_VAR"))

// expansion
env.expand("${MY_VAR}"); // test
env.expand("${NO_VALUE:-default}"); // default
console.log(env.get("NO_VALUE")); // undefined

env.expand("${NO_VALUE:=default}"); // default
console.log(env.get("NO_VALUE")); // default

try {
    env.expand("${REQUIRED_VAR:?Environment variable REQUIRED_VAR is missing}");
} catch(e) {
    console.log(e.message); // Environment variable REQUIRED_VAR is missing
}

// proxy object to allow get/set/delete similar to process.env
console.log(env.values.MY_VAR);

// undefined will remove a value
env.merge({
    "VAR2": "VALUE",
    "MY_VAR": undefined
});

env.set("MY_VAR", "test")
env.remove("MY_VAR");

// append to the end of the environment path variables
env.path.append("/opt/test/bin");

// prepends the path
env.path.prepend("/opt/test2/bin");
env.path.has("/opt/test2/bin");

// removes the path. on windows this is case insensitive.
env.path.remove("/opt/test2/bin");

// replaces the path.
env.path.replace("/opt/test/bin", "/opt/test2/bin")

console.log(env.path.split());
console.log(env.path) // the full path string
console.log(env.path.toString()) // the full path string

const path = env.path.get()

// overwrites the environment's PATH variable
env.path.overwite(`${PATH}:/opt/test4/bin`)

MIT License

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@gnome/env

Import symbol

import * as env from "@gnome/env";
or

Import directly with a jsr specifier

import * as env from "jsr:@gnome/env";

Add Package

pnpm i jsr:@gnome/env
or (using pnpm 10.8 or older)
pnpm dlx jsr add @gnome/env

Import symbol

import * as env from "@gnome/env";

Add Package

yarn add jsr:@gnome/env
or (using Yarn 4.8 or older)
yarn dlx jsr add @gnome/env

Import symbol

import * as env from "@gnome/env";

Add Package

vlt install jsr:@gnome/env

Import symbol

import * as env from "@gnome/env";

Add Package

npx jsr add @gnome/env

Import symbol

import * as env from "@gnome/env";

Add Package

bunx jsr add @gnome/env

Import symbol

import * as env from "@gnome/env";