JSON to Env

JSON to Env

Pull your env remotely from the cloud.

ยท

3 min read

A Backend programmer who has lost their device in the past will understand the stress it takes to re-create the development env files for your projects. ๐Ÿ˜ข

Apart from sad scenarios like this, there is always no sync in changes that happen with env files amongst teams. In some cases, you will probably run the program first or check an env declaration file if it exists for guidelines on how to create your env file. These guidelines do not include the env values or API keys ๐Ÿคฏ

These situations can be prevented by using the jsonbank-env cli package.

Requirements
  • Nodejs Installed.
  • Jsonbank account
  • Jsonbank public key.

Getting Started.

Create a new private project on jsonbank or use any existing private project. For this post, we will be using a new project we created called envs

We will also create 3 new files

  • default.json
  • default-array.json
  • prod.json

default.json

{
  "NODE_ENV": "development",
  "PORT": 3000,
  "SSL": null,
  "DB_HOST": "localhost",
  "DB_USER": "root",
  "DB_PASS": "root",
  "DB_NAME": "test"
}

default-array.json

[
  {
    "NODE_ENV": "development",
    "PORT": 3000
  },
  {
    "SSL": null
  },
  {
    "DB_HOST": "localhost",
    "DB_USER": "root",
    "DB_PASS": "root",
    "DB_NAME": "test"
  }
]

prod.json

{
  "NODE_ENV": "production",
  "PORT": 80,
  "SSL": true,
  "DB_HOST": "localhost",
  "DB_USER": "root",
  "DB_PASS": "root",
  "DB_NAME": "test"
}

So your project menu should be looking like this

Screenshot 2022-10-29 at 9.50.24 PM.png

Generate Config File

Navigate to your local working directory where you want the envs to be imported.

Run

npx jsonbank-env init

This will create your jsonbank.env.json file. This file is where you will set your public key and remote paths to your env files.

Get your Public key

Create a new application on jsonbank and you will be given your public and private keys. Put the public key in your jsonbank.env.json

Set your remote files

Set your config file to look like this

{
  "public_key": "Your public key",
  "envs": {
      "dev": "envs/default.json",
      "dev:formatted: "envs/default-array.json",
      "prod": "envs/prod.json"
   }
}

Fetch Env Files

Get the dev env file and save it as .env

npx jsonbank-env dev .env

Your generated env file should look like

NODE_ENV="development"
PORT=3000
SSL=
DB_HOST="localhost"
DB_USER="root"
DB_PASS="root"
DB_NAME="test"

Get the dev:formatted env file and save it as .neat.env

npx jsonbank-env dev:formatted .neat.env

Your generated env file should look like this with neat spacing because the json content is an array of objects. Each object in the array is automatically separated with a new line.

NODE_ENV="development"
PORT=3000

SSL=

DB_HOST="localhost"
DB_USER="root"
DB_PASS="root"
DB_NAME="test"

Now try the last one for prod env file. it should generate an env with NODE_ENV=production

We hope you find this service as useful as we did. For more info, see the npm package documentation

ย