What is env-cmd?
The env-cmd npm package allows you to easily set environment variables from a file or inline before running a command. This is particularly useful for managing different environments (development, testing, production) without hardcoding environment variables in your code.
What are env-cmd's main functionalities?
Load environment variables from a file
This feature allows you to load environment variables from a specified file before running your command. The file should follow the standard .env format.
env-cmd -f ./path/to/.env node app.js
Load environment variables from multiple files
You can load environment variables from multiple files. This is useful for layering configurations, such as having a base configuration and an environment-specific override.
env-cmd -f ./path/to/.env -f ./path/to/.env.local node app.js
Inline environment variables
This feature allows you to set environment variables inline directly in the command. This is useful for quick tests or temporary overrides.
env-cmd -e 'NODE_ENV=production' node app.js
JSON configuration
You can also pass environment variables as a JSON object. This is useful for more complex configurations that might be easier to manage in JSON format.
env-cmd -e '{"NODE_ENV": "production", "API_KEY": "12345"}' node app.js
Other packages similar to env-cmd
dotenv
dotenv is a popular package for loading environment variables from a .env file into process.env. Unlike env-cmd, dotenv is typically used by requiring it in your code, rather than as a command-line tool.
cross-env
cross-env allows you to set environment variables across different platforms (Windows, Linux, macOS) in a consistent manner. It is similar to env-cmd but focuses more on cross-platform compatibility.
dotenv-cli
dotenv-cli is a command-line interface for dotenv, allowing you to load environment variables from a .env file before running a command, similar to env-cmd. It provides a simpler interface but lacks some of the advanced features of env-cmd.
env-cmd
A simple node program for executing commands using an environment from an env file.
Install
npm install env-cmd
Usage
Environment file ./test/.env
# This is a comment
ENV1=THANKS # Yay inline comments support
ENV2=FOR ALL
ENV3 THE FISH # This format is also accepted
# Surround value in double quotes when using a # symbol in the value
ENV4="ValueContains#Symbol"
# If using double quotes as part of the value, you must surround the value in double quotes
ENV5=""Value includes double quotes""
Package.json
{
"scripts": {
"test": "env-cmd ./test/.env mocha -R spec"
}
}
or
Terminal
./node_modules/.bin/env-cmd ./test/.env node index.js
or
.env-cmdrc file .env-cmdrc
{
"development": {
"ENV1": "Thanks",
"ENV2": "For All"
},
"production": {
"ENV1": "The Fish"
}
}
./node_modules/.bin/env-cmd production node index.js
Environment File Formats
These are the currently accepted environment file formats. If any other formats are desired please create an issue.
key=value
key value
- Key/value pairs as JSON
- JavaScript file exporting an object
.env-cmdrc
file (as valid json) in execution directory
Why
Because sometimes its just too cumbersome passing lots of environment variables to scripts. Its 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!
Related Projects
cross-env
- Cross platform setting of environment scripts
Special Thanks
Special thanks to cross-env
for inspiration (use's the same cross-spawn
lib underneath too).
Contributors
- Eric Lanehart
- Jon Scheiding
Contributing Guide
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 formats according to js-standardnpm test
make sure all tests passnpm run test-cover
make sure the coverage has not decreased from current master