Files
pnpm/pnpm11/config/env-replace/README.md
Zoltan Kochan ad525f26db refactor: move the pnpm/components packages into the monorepo (#16214)
The remaining packages of the pnpm/components Bit workspace now live
under pnpm11/ and are published from here, keeping their npm names:

  @pnpm/config.env-replace           -> pnpm11/config/env-replace
  @pnpm/log.group                    -> pnpm11/log/group
  @pnpm/network.agent                -> pnpm11/network/agent
  @pnpm/network.ca-file              -> pnpm11/network/ca-file
  @pnpm/network.config               -> pnpm11/network/config
  @pnpm/network.proxy-agent          -> pnpm11/network/proxy-agent
  @pnpm/os.env.path-extender         -> pnpm11/os/env/path-extender
  @pnpm/os.env.path-extender-posix   -> pnpm11/os/env/path-extender-posix
  @pnpm/os.env.path-extender-windows -> pnpm11/os/env/path-extender-windows

lex-comparator and nerf-dart were already moved under new names, and the
Bit env component is build tooling with no counterpart here.

The sources are carried over from pnpm/components at 42ec0f7, which
includes the Windows fix that rewrites a PNPM_HOME value stored with the
wrong registry type. pnpm v12 got the same fix in pnpm/pnpm#15746, so
its changeset now also covers pnpm v11, which bundles the workspace copy.

Porting to the monorepo's ES module setup:

- relative imports carry .js and type re-exports are marked `type`;
- tests use jest.unstable_mockModule and import through the package;
- string.prototype.matchall and graceful-fs give way to built-ins;
- safe-execa and lru-cache move to the catalog versions (0.3, 11);
- network.config depends on @pnpm/config.registry-auth-key;
- the strict-ssl proxy test uses node:https and proxy v4 instead of
  node-fetch and proxy v1.

The packages start at 1100.0.0 to sit in the pnpm11 version band.
2026-09-26 23:33:47 +02:00

1.8 KiB

@pnpm/config.env-replace

Replaces env variable placeholders in configuration settings

envReplace

Strict mode — throws when a ${VAR} placeholder has no value and no default.

function envReplace(settingValue: string, env: NodeJS.ProcessEnv): string;
import { envReplace } from '@pnpm/config.env-replace'

envReplace('${foo}', process.env)

envReplaceLossy

Lossy mode — replaces unresolved ${VAR} placeholders with '' and reports them in unresolved instead of throwing. Resolvable placeholders and ${VAR-default} / ${VAR:-default} fallbacks elsewhere in the same string still expand normally; only the genuinely unresolved bare ones are dropped.

Use this when leaving the literal ${VAR} in the substituted value would be worse than dropping it (e.g. auth tokens in .npmrc under OIDC trusted publishing).

function envReplaceLossy(
  settingValue: string,
  env: NodeJS.ProcessEnv
): { value: string; unresolved: string[] };
import { envReplaceLossy } from '@pnpm/config.env-replace'

const { value, unresolved } = envReplaceLossy('${foo}-${missing}', process.env)
// value:      'foo_value-'
// unresolved: ['${missing}']

Placeholder syntax

  • ${NAME} — strict; the env var must be set (otherwise envReplace throws, or envReplaceLossy substitutes '' and records the placeholder).
  • ${NAME-fallback} — fallback if NAME is unset.
  • ${NAME:-fallback} — fallback if NAME is unset or an empty string.
  • A leading \ escapes the placeholder (\${NAME} stays literal).

{ NAME: undefined } in env is treated as unset for both functions — the same as the key being absent. This matches the Record<string, string | undefined> shape of NodeJS.ProcessEnv, where callers (notably tests) routinely model an unset variable as a present-but-undefined property.

License

MIT