Gathering detailed insights and metrics for env-cmd
Gathering detailed insights and metrics for env-cmd
npm install env-cmd
Typescript
Module System
Min. Node Version
Node Version
NPM Version
98.1
Supply Chain
100
Quality
75.8
Maintenance
100
Vulnerability
100
License
TypeScript (96.97%)
JavaScript (3.03%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
171,836,999
Last Day
224,062
Last Week
1,153,930
Last Month
4,900,140
Last Year
51,523,883
MIT License
1,770 Stars
194 Commits
68 Forks
14 Watchers
5 Branches
22 Contributors
Updated on Feb 14, 2025
Minified
Minified + Gzipped
Latest Version
10.1.0
Package Id
env-cmd@10.1.0
Size
13.75 kB
NPM Version
6.13.4
Node Version
12.14.1
Published on
Feb 10, 2020
Cumulative downloads
Total Downloads
Last Day
3.8%
224,062
Compared to previous day
Last Week
3%
1,153,930
Compared to previous week
Last Month
35.5%
4,900,140
Compared to previous month
Last Year
13.3%
51,523,883
Compared to previous year
A simple node program for executing commands using an environment from an env file.
npm install env-cmd
or npm install -g env-cmd
Environment file ./.env
1# This is a comment 2ENV1=THANKS 3ENV2=FOR ALL 4ENV3=THE FISH
Package.json
1{ 2 "scripts": { 3 "test": "env-cmd mocha -R spec" 4 } 5}
Terminal
1./node_modules/.bin/env-cmd node index.js
To use a custom env filename or path, pass the -f
flag. This is a major breaking change from prior versions < 9.0.0
Terminal
1./node_modules/.bin/env-cmd -f ./custom/path/.env node index.js
1Usage: _ [options] <command> [...args] 2 3Options: 4 -v, --version output the version number 5 -e, --environments [env1,env2,...] The rc file environment(s) to use 6 -f, --file [path] Custom env file path (default path: ./.env) 7 --fallback Fallback to default env file path, if custom env file path not found 8 --no-override Do not override existing environment variables 9 -r, --rc-file [path] Custom rc file path (default path: ./.env-cmdrc(|.js|.json) 10 --silent Ignore any env-cmd errors and only fail on executed program failure. 11 --use-shell Execute the command in a new shell with the given environment 12 --verbose Print helpful debugging information 13 -x, --expand-envs Replace $var in args and command with environment variables 14 -h, --help output usage information
.rc
file usageFor more complex projects, a .env-cmdrc
file can be defined in the root directory and supports
as many environments as you want. Simply use the -e
flag and provide which environments you wish to
use from the .env-cmdrc
file. Using multiple environment names will merge the environment variables
together. Later environments overwrite earlier ones in the list if conflicting environment variables
are found.
.rc file ./.env-cmdrc
1{ 2 "development": { 3 "ENV1": "Thanks", 4 "ENV2": "For All" 5 }, 6 "test": { 7 "ENV1": "No Thanks", 8 "ENV3": "!" 9 }, 10 "production": { 11 "ENV1": "The Fish" 12 } 13}
Terminal
1./node_modules/.bin/env-cmd -e production node index.js 2# Or for multiple environments (where `production` vars override `test` vars, 3# but both are included) 4./node_modules/.bin/env-cmd -e test,production node index.js
--no-override
optionPrevents overriding of existing environment variables on process.env
and within the current
environment.
--fallback
file usage optionIf the .env
file does not exist at the provided custom path, then use the default
fallback location ./.env
env file instead.
--use-shell
Executes the command within a new shell environment. This is useful if you want to string multiple commands together that share the same environment variables.
Terminal
1./node_modules/.bin/env-cmd -f ./test/.env --use-shell "npm run lint && npm test"
EnvCmd supports reading from asynchronous .env
files. Instead of using a .env
file, pass in a .js
file that exports either an object or a Promise
resolving to an object ({ ENV_VAR_NAME: value, ... }
). Asynchronous .rc
files are also supported using .js
file extension and resolving to an object with top level environment
names ({ production: { ENV_VAR_NAME: value, ... } }
).
Terminal
1./node_modules/.bin/env-cmd -f ./async-file.js node index.js
-x
expands vars in argumentsEnvCmd supports expanding $var
values passed in as arguments to the command. The allows a user
to provide arguments to a command that are based on environment variable values at runtime.
Terminal
1# $USER will be expanded into the env value it contains at runtime 2./node_modules/.bin/env-cmd -x node index.js --user=$USER
--silent
suppresses env-cmd errorsEnvCmd supports the --silent
flag the suppresses all errors generated by env-cmd
while leaving errors generated by the child process and cli signals still usable. This
flag is primarily used to allow env-cmd
to run in environments where the .env
file might not be present, but still execute the child process without failing
due to a missing file.
You can find examples of how to use the various options above by visiting the examples repo env-cmd-examples.
These are the currently accepted environment file formats. If any other formats are desired please create an issue.
.env
as key=value
.env.json
Key/value pairs as JSON.env.js
JavaScript file exporting an object
or a Promise
that resolves to an object
.env-cmdrc
as valid json or .env-cmdrc.json
in execution directory with at least one environment { "dev": { "key1": "val1" } }
.env-cmdrc.js
JavaScript file exporting an object
or a Promise
that resolves to an object
that contains at least one environmentThis lib attempts to follow standard bash
path rules. The rules are as followed:
Home Directory = /Users/test
Working Directory = /Users/test/Development/app
Type | Input Path | Expanded Path |
---|---|---|
Absolute | /some/absolute/path.env | /some/absolute/path.env |
Home Directory with ~ | ~/starts/on/homedir/path.env | /Users/test/starts/on/homedir/path.env |
Relative | ./some/relative/path.env or some/relative/path.env | /Users/test/Development/app/some/relative/path.env |
Relative with parent dir | ../some/relative/path.env | /Users/test/Development/some/relative/path.env |
EnvCmd
A function that executes a given command in a new child process with the given environment and options
options
{ object
}
command
{ string
}: The command to execute (node
, mocha
, ...)commandArgs
{ string[]
}: List of arguments to pass to the command
(['-R', 'Spec']
)envFile
{ object
}
filePath
{ string
}: Custom path to .env file to read from (defaults to: ./.env
)fallback
{ boolean
}: Should fall back to default ./.env
file if custom path does not existrc
{ object
}
environments
{ string[]
}: List of environment to read from the .rc
filefilePath
{ string
}: Custom path to the .rc
file (defaults to: ./.env-cmdrc(|.js|.json)
)options
{ object
}
expandEnvs
{ boolean
}: Expand $var
values passed to commandArgs
(default: false
)noOverride
{ boolean
}: Prevent .env
file vars from overriding existing process.env
vars (default: false
)silent
{ boolean
}: Ignore any errors thrown by env-cmd, used to ignore missing file errors (default: false
)useShell
{ boolean
}: Runs command inside a new shell instance (default: false
)verbose
{ boolean
}: Prints extra debug logs to console.info
(default: false
)Promise<object>
}: key is env var name and value is the env var valueGetEnvVars
A function that parses environment variables from a .env
or a .rc
file
options
{ object
}
envFile
{ object
}
filePath
{ string
}: Custom path to .env file to read from (defaults to: ./.env
)fallback
{ boolean
}: Should fall back to default ./.env
file if custom path does not existrc
{ object
}
environments
{ string[]
}: List of environment to read from the .rc
filefilePath
{ string
}: Custom path to the .rc
file (defaults to: ./.env-cmdrc(|.js|.json)
)verbose
{ boolean
}: Prints extra debug logs to console.info
(default: false
)Promise<object>
}: key is env var name and value is the env var valueBecause sometimes it is just too cumbersome passing a lot of environment variables to scripts. It is usually just easier to have a file with all the vars in them, especially for development and testing.
????Do not commit sensitive environment data to a public git repo! ????
cross-env
- Cross platform setting of environment scripts
Special thanks to cross-env
for inspiration (uses the
same cross-spawn
lib underneath too).
I welcome all pull requests. Please make sure you add appropriate test cases for any features added. Before opening a PR please make sure to run the following scripts:
npm run lint
checks for code errors and format according to ts-standardnpm test
make sure all tests passnpm run test-cover
make sure the coverage has not decreased from current masterNo vulnerabilities found.
Reason
30 commit(s) and 25 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 5/7 approved changesets -- score normalized to 7
Reason
branch protection is not maximal on development and all release branches
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More