This page gives you four fast on‑ramps. Each section includes a minimal snippet and links to the right deep‑dive. Requires Node.js 22.12 or later.
Upgrading from 7.x? See Migrating to 8.0.
z@karmaniverous/get-dotenv/cliHost no longer re-exports z (removed in 8.0.0). Import { z } from zod directly, and list zod in your own dependencies.Use the parent alias so flags apply to getdotenv, not the inner command. Prefer single quotes to avoid outer‑shell expansion.
npx @karmaniverous/get-dotenv -c 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
Tips:
--trace [keys...] and --redact. Set GETDOTENV_STDIO=pipe or pass --capture for CI‑friendly buffering./bin/bash on POSIX and powershell.exe on Windows; see Shell execution behavior.Compose env from dotenv files and overlays in code.
import { getDotenv } from '@karmaniverous/get-dotenv';
const vars = await getDotenv({
env: 'dev',
paths: ['./'],
// dynamicPath: './.env.js' or pass dynamic: defineDynamic({...})
});
console.log(vars.APP_SETTING);
Next:
getdotenv.config.json|yaml|js|ts using Config files and overlays.dynamic or dynamicPath for computed values; see Dynamic variables (JS/TS).as const) to keep inference strong without casts.Prefer the named factory to get a small runner with the shipped plugins installed.
#!/usr/bin/env node
import { createCli } from '@karmaniverous/get-dotenv/cli';
await createCli({ alias: 'toolbox', importMetaUrl: import.meta.url })();
Notes:
importMetaUrl is required (createCli() without it throws). The nearest package.json above it is your CLI's host package, which supplies:
getdotenv.config.* beside that package.json (it replaces get-dotenv's own packaged layer; there is no third layer);-V/--version output and the help header version (package.json version, unless you pass version);package.json description, unless you pass description).getdotenv.config.* applies once, as the project config.createCli installs cmd, batch, aws, and init by default. -h/--help prints help and returns. See Shell execution behavior and Shipped Plugins.CommonJS usage (dynamic import):
(async () => {
// The package is ESM-only; use dynamic import from CommonJS.
const { createCli } = await import('@karmaniverous/get-dotenv/cli');
// Build and run your CLI with args. CommonJS has no import.meta.url, so
// pass this module's file URL.
const { pathToFileURL } = require('node:url');
const run = createCli({
alias: 'toolbox',
importMetaUrl: pathToFileURL(__filename).href,
});
await run(['-h']);
})();
Use init to copy templates and a host‑based CLI skeleton into your project.
# JSON config + .local + CLI named "acme"
npx @karmaniverous/get-dotenv init . \
--config-format json \
--with-local \
--cli-name acme \
--force
or a TypeScript config with dynamic examples:
npx @karmaniverous/get-dotenv init ./apps/toolbox \
--config-format ts \
--cli-name toolbox
Notes:
--force. See init.__CLI_NAME__ tokens are replaced with your chosen name.Minimal skeleton (DX‑friendly; no explicit types or casts):
#!/usr/bin/env node
import { createCli } from '@karmaniverous/get-dotenv/cli';
import { helloPlugin } from './plugins/hello';
const run = createCli({
alias: '__CLI_NAME__',
importMetaUrl: import.meta.url,
compose: (p) => p.use(helloPlugin()),
});
await run();