Skip to content
CLDMVPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@cldmv/envm

npm version license size npm-downloads github-downloads

@cldmv/envm is a modern, cross-platform environment variable manager for Node.js projects. Designed for both developers and automation, it lets you safely read, write, and manipulate environment variables on Windows and POSIX systems—without ever touching a class. With robust backup and restore features, a powerful CLI, and a clean, ESM-first API, envm makes managing your environment variables simple, safe, and scriptable. Whether you're tweaking your PATH, rolling back a bad change, or automating setup across platforms, envm gives you the control and confidence you need.

✨ What's New

Latest: v1.2.0 (November 2026)

  • require() returns envManager itself — require("@cldmv/envm").set(…) works, and .envManager, .default and const { envManager } = require(…) all point at the same object, so v1.1.0 code keeps working (#87, #89). dist/ is now one file per module instead of a bundle, and the CLI no longer ships its own copy of the library (#97).
  • Bug fixes — an unknown or missing scope is refused instead of silently doing nothing, or writing system scope on Windows (#94). The CLI no longer stores a --flag as a value or edits the wrong variable when --name is left out (#94), and backup.purge() deletes backups older than maxAgeDays (#93).
  • View full v1.2.0 Changelog

Recent Releases

  • v1.1.0 (November 2026) — the CLDMV library layout with a require() entry, type declarations and the envm-dev condition, plus fixes for the installed envm command, Windows rollback and user scope on Linux and macOS (#59, #60, #61, #62, #68, #69) (Changelog)
  • v1.0.12 (October 2026) — @cldmv/vitest-runner 1.5.3, vitest and @vitest/coverage-v8 5.0.3; dev-only, no library or CLI code changed (#42, #43, #44) (Changelog)
  • v1.0.11 (October 2026) — @cldmv/fix-headers 2.2.0 and @cldmv/configs 1.2.4; every header already matched, so nothing was restamped (#34, #40) (Changelog)
  • v1.0.10 (October 2026) — the CI ✅ Required PR Check mirror job runs on every path instead of being skipped on in-repo PRs (#32) (Changelog)

📚 For complete version history, see docs/changelog/ and the GitHub Releases.

Features

  • Cross-platform: Works on Windows (registry) and POSIX (dotfiles, /etc/environment)
  • No classes: API is plain objects and functions
  • ESM and CommonJS: an ESM build, plus a require() entry that loads the same build (no second copy)
  • PATH-like helpers: Manipulate PATH and similar variables safely
  • Backup/restore: Automatic, timestamped backups with purge and rollback
  • CLI: Powerful envm command for scripting and automation
  • TypeScript-friendly: JSDoc-annotated API with generated type declarations
  • Lightweight: about 15 KB minified (core + CLI)

Installation

npm install @cldmv/envm

Requires Node.js 14.14.0 or later for import. require() needs Node.js ^20.19.0 or >=22.13.0.

Developing this package needs Node.js 22.22.0 or later (devEngines).

Usage

Node.js API

import { envManager } from "@cldmv/envm";

// Get a variable (expanded)
const value = await envManager.getExpanded("PATH", { scope: "user" });

// Set a variable
await envManager.set("MY_VAR", "value", { scope: "user" });

// Unset a variable
await envManager.unset("MY_VAR", { scope: "user" });

// PATH helpers
await envManager.path.prepend({ name: "PATH", scope: "user", values: ["/usr/local/bin"] });
await envManager.path.append({ name: "PATH", scope: "user", values: ["/opt/bin"] });
await envManager.path.remove({ name: "PATH", scope: "user", values: ["/opt/bin"] });
await envManager.path.sort({ name: "PATH", scope: "user" });
await envManager.path.unique({ name: "PATH", scope: "user" });

// Backup helpers
await envManager.backup.list({ scope: "user" });
await envManager.backup.restore("user-PATH-2025-08-11T12-00-00-000Z.bak");
await envManager.backup.purge({ maxPerScope: 10, maxAgeDays: 7 });

CommonJS

require() returns the envManager object itself, the same one import { envManager } gives you:

const envManager = require("@cldmv/envm");
await envManager.set("MY_VAR", "value", { scope: "user" });

const { envManager } = require("@cldmv/envm") and require("@cldmv/envm").default work too; both point at the same object.

The CommonJS entry loads the ESM build through Node's require(esm). On a Node.js version without it, require() throws ERR_REQUIRE_ESM with the supported range; use import() there instead.

CLI

npx envm get --name PATH --scope user
npx envm set --name MY_VAR --value hello --scope user
npx envm unset --name MY_VAR --scope user
npx envm path prepend --name PATH --values /usr/local/bin --scope user
npx envm backup list --scope user
npx envm backup restore user-PATH-2025-08-11T12-00-00-000Z.bak
npx envm --version

CLI Options

  • --name/-n — Variable name
  • --value/-v — Value to set
  • --scope — session, user, or system
  • --raw — Get raw value (no expansion)
  • --expanded — Get expanded value
  • --backup — Enable/disable backup (default: true)
  • --verify — Enable/disable verification (default: true)
  • --dry-run — Simulate changes

PATH Subcommands

  • prepend, append, remove, sort, unique, get

Backup Subcommands

  • list, restore

API Reference

See JSDoc in source for full details. Key methods:

  • envManager.getRaw(name, { scope })
  • envManager.getExpanded(name, { scope })
  • envManager.set(name, value, { scope, backup, verify, rollbackOnFail })
  • envManager.unset(name, { scope, backup, verify, rollbackOnFail })
  • envManager.path.{get,prepend,append,remove,sort,unique}({ ... })
  • envManager.backup.{list,restore,setBackupDir,getBackupDir,purge}({ ... })

Scopes

  • session: Only affects current process
  • user: User profile (Windows registry or ~/.profile)
  • system: System-wide (Windows registry or /etc/environment)

Safety

  • All changes to user/system env are backed up (unless --backup false)
  • Rollback is attempted on failure if backup is enabled
  • Backups are timestamped and auto-purged

Development

The package entry points ("." and "./cli") resolve to the built dist/ files by default. In a checkout, the envm-dev export condition switches them to src/:

npm run build                                     # esbuild, one file per src module → dist/ (index.mjs, index.cjs, cli.mjs, ...)
node --conditions=envm-dev app.mjs                # run against src/
export NODE_OPTIONS=--conditions=envm-dev         # or set it once per shell

Without the condition and without a build, importing the package fails with ERR_MODULE_NOT_FOUND for dist/. npm test runs the Vitest suite against src/ (the config passes the condition), then builds and checks the dist/ entries with node --test. npm run test:dist builds and runs the Vitest suite against dist/ instead of src/.

License

Apache-2.0 © Shinrai / CLDMV

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages