exec-with-env
v1.1.1
Published
A simple CLI tool to run commands with environment variables loaded from a file (like `.env`).
Readme
exec-with-env
A simple CLI tool to run commands with environment variables loaded from a file (like .env).
Features
- Loads environment variables from a specified file and merges them with the current environment.
- File-based variables take precedence over existing environment variables.
- Simple CLI interface, suitable for scripting and CI/CD.
Installation
You can use npx to run without installing:
npx exec-with-env -f <envfile> <command> [args...]Or clone this repo and run locally:
git clone <this-repo-url>
cd exec-with-env
node index.js -f <envfile> <command> [args...]Usage
npx exec-with-env -f <envfile> <command> [args...]-f <envfile>: (optional) Path to the environment file. Defaults to.envif not specified.-r: (optional) Replace variables like$VARNAMEor${VARNAME}in command arguments with their values from the environment.<command> [args...]: The command to run with the loaded environment variables.
Examples
Run a script with variables from .env:
npx exec-with-env -f .env node myscript.jsRun a command with a custom env file:
npx exec-with-env -f config.env echo $MY_VARReplace variables in arguments (e.g., $HELLO) with values from the env file:
npx exec-with-env -f .env -r echo $HELLOIf you have .env containing HELLO=world, this will output:
worldIf -f is omitted, .env in the current directory is used:
npx exec-with-env node myscript.jsError Handling
- If the env file is missing, an error is printed and the process exits with a non-zero code.
- If the command is missing, usage instructions are printed.
Testing
This project uses Node's built-in test runner. To run tests:
npm testLicense
MIT
