dotenv-json-complex

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dotenv-json-complex

Load complex enviroment variable via JSON file

Version
1.0.2
Version published
Maintainers
1
Created

dotenv-json-complex

Load complex enviroment variable via JSON file

Installation

With Nodejs installed run the installation command in a terminal:

npm install dotenv-json-complex

Usage

Create a .env.json file in the root of your project:

{
    "my_app_env": {
      "nodeEnv": "development",
      "api": {
        "prefix": "/api"
      },
      "database": {
        "username": "foo",
        "password": "bar",
        "name": "awesome_db_name",
        "host": "my_db_host"
      }
    }
}

After that, load your environment variables by requiring the dotenv dotenv-json-complex and executing it:

// here you load the json to your process.env
require("dotenv-json-complex")();

const env = JSON.parse(process.env.my_app_env);

// this will print your complex json environment!
console.log(env)

// like this
{
  "nodeEnv": "development",
  "api": {
    "prefix": "/api"
  },
  "database": {
    "username": "foo",
    "password": "bar",
    "name": "awesome_db_name",
    "host": "my_db_host"
  }
}

Furthermore you can provide an object to change environment filename:

require("dotenv-json-complex")({ environment: 'qa' });

This will look for .env.qa.json file and load it in memory.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

FAQs

Package last updated on 16 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts