2.0.0 • Published 7 years ago
multi-env v2.0.0
multi-env
Multi-env merges and loads environment variables from multiple .env files for nodejs projects.
Multi-env is built on dotenv and follows the same rules.
Installation
npm install --save multi-envConfiguration
Add configuration for multi-env to your package.json:
{
"name": "my-package",
"version": "0.1.0",
"scripts": {
"start": "node ./src/index.js"
},
"config": {
"multi-env": {
"files": [
"local.env",
"shared.env"
]
}
}
}files should be an array of .env file paths (relative to package.json).
When the same variable is assigned in multiple files, the earliest file listed in your configuration will take precedence. Environment variables that have already been set will never be modified.
Usage
In Application Code
// src/index.js
require('multi-env')();
console.info(process.env.MY_VARIABLE_FROM_DOTENV);
...Preload
You can use the node --require (-r) command line option to preload multi-env. By doing this, you do not need to require and execute multi-env in your application code.
{
"name": "my-package",
"version": "0.1.0",
"scripts": {
"start": "node -r multi-env/config ./src/index.js"
},
"config": {
"multi-env": {
"files": [
"local.env",
"shared.env"
]
}
}
}// src/index.js
console.info(process.env.MY_VARIABLE_FROM_DOTENV);
...