env-cmd is a simple yet powerful Node.js tool for executing commands using environment variables loaded from various file formats. It supports both command-line usage and programmatic API access, with comprehensive file format support including .env, .env.json, .env.js, .env.ts, and multi-environment .env-cmdrc files.
npm install env-cmd or npm install -g env-cmdimport { EnvCmd, CLI, GetEnvVars, type Environment, type EnvCmdOptions } from "env-cmd";For CommonJS:
const { EnvCmd, CLI, GetEnvVars } = require("env-cmd");Note: Additional utility functions like expandEnvs, resolveEnvFilePath, etc. are available via direct submodule imports (e.g., import { expandEnvs } from "env-cmd/dist/expand-envs.js").
# Use default .env file
env-cmd -- node server.js
# Use custom env file
env-cmd -f .env.production -- node server.js
# Use RC file with environments
env-cmd -e staging,production -- node server.js
# Enable environment variable expansion
env-cmd --expand-envs -- echo "API URL: $API_URL"import { EnvCmd, GetEnvVars } from "env-cmd";
// Execute command with environment variables
const env = await EnvCmd({
command: "node",
commandArgs: ["server.js"],
envFile: { filePath: ".env.production" },
options: {
verbose: true,
expandEnvs: true
}
});
// Just load environment variables
const envVars = await GetEnvVars({
envFile: { filePath: ".env" },
verbose: true
});env-cmd is built around several key components:
EnvCmd function spawns processes with loaded environment variablesMain functionality for executing commands with environment variables loaded from files. Supports process spawning, signal handling, and variable expansion.
function EnvCmd(options: EnvCmdOptions): Promise<Environment>;
interface EnvCmdOptions {
command: string;
commandArgs: string[];
envFile?: EnvFileOptions;
rc?: RCFileOptions;
options?: {
expandEnvs?: boolean;
recursive?: boolean;
noOverride?: boolean;
silent?: boolean;
useShell?: boolean;
verbose?: boolean;
};
}System for loading environment variables from various file formats with fallback support and error handling.
function getEnvVars(options?: GetEnvVarOptions): Promise<Environment>;
function getEnvFile(options: { filePath?: string, fallback?: boolean, verbose?: boolean }): Promise<Environment>;
function getRCFile(options: { environments: string[], filePath?: string, verbose?: boolean }): Promise<Environment>;
interface GetEnvVarOptions {
envFile?: EnvFileOptions;
rc?: RCFileOptions;
verbose?: boolean;
}CLI system with comprehensive argument parsing and command execution integration.
function CLI(args: string[]): Promise<Environment>;
function parseArgs(args: string[]): EnvCmdOptions;Support for multiple environment file formats including .env, JSON, JavaScript, TypeScript, and RC files.
function getEnvFileVars(envFilePath: string): Promise<Environment>;
function getRCFileVars(options: { environments: string[], filePath: string }): Promise<Environment>;Helper functions for path resolution, argument parsing, and environment variable expansion.
function expandEnvs(str: string, envs: Environment): string;
function resolveEnvFilePath(userPath: string): string;
function parseArgList(list: string): string[];type Environment = Partial<Record<string, string>>;
type RCEnvironment = Partial<Record<string, Environment>>;
interface EnvFileOptions {
filePath?: string;
fallback?: boolean;
}
interface RCFileOptions {
environments: string[];
filePath?: string;
}
interface GetEnvVarOptions {
envFile?: EnvFileOptions;
rc?: RCFileOptions;
verbose?: boolean;
}
interface EnvCmdOptions extends GetEnvVarOptions {
command: string;
commandArgs: string[];
options?: {
expandEnvs?: boolean;
recursive?: boolean;
noOverride?: boolean;
silent?: boolean;
useShell?: boolean;
verbose?: boolean;
};
}
type CommanderOptions = Command<[], {
environments?: true | string[];
expandEnvs?: boolean;
recursive?: boolean;
fallback?: boolean;
file?: true | string;
override?: boolean;
silent?: boolean;
useShell?: boolean;
verbose?: boolean;
}>;./.env, ./.env.js, ./.env.json./.env-cmdrc, ./.env-cmdrc.js, ./.env-cmdrc.json