dotenv-json-complex

1.0.4 • Public • Published

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

Package Sidebar

Install

npm i dotenv-json-complex

Weekly Downloads

1,145

Version

1.0.4

License

MIT

Unpacked Size

5.17 kB

Total Files

6

Last publish

Collaborators

  • mlucascardoso