Packages
@nodevisor/env
Read and set environment variables on local and remote systems.
Install
npm install @nodevisor/env
Provides methods to read, set, and manage environment variables on the target system. Supports loading .env files.
Quick Start
import $ from '@nodevisor/shell';
import Env from '@nodevisor/env';
const env = $(Env);
// Get an environment variable
const home = await env.home();
console.log(home); // "/root"
// Set a variable
await env.set('NODE_ENV', 'production');
// Read it back
const nodeEnv = await env.get('NODE_ENV');
console.log(nodeEnv); // "production"
// Load from .env file
await env.load('/opt/app/.env');
API
get(name)
Read an environment variable from the system.
const path = await $(Env).get('PATH');
set(name, value)
Set an environment variable in the shell context.
await $(Env).set('MY_VAR', 'hello');
unset(name)
Remove an environment variable.
await $(Env).unset('MY_VAR');
home()
Get the home directory of the current user.
const home = await $(Env).home();
// Linux/macOS: "/home/user" or "/root"
// Windows: "C:\Users\user"
load(path)
Load environment variables from a .env file into the shell context.
await $(Env).load('/opt/app/.env');
Shell-Level Env (CommandBuilder)
The CommandBuilder also supports per-command environment variables:
import $ from '@nodevisor/shell';
// Set env for a specific command
const cmd = $`echo $MY_VAR`;
cmd.setEnv('MY_VAR', 'hello');
await cmd.text(); // "hello"
// Add an env file
cmd.addEnvFile('.env');
See the Shell package docs for more details.
Related Packages
@nodevisor/shell— CommandBuilder env support@nodevisor/authorized-keys— Uses Env to resolve home directory