Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define array/object in .env file?

Tags:

dotenv

The following is my Javascript object:

window.options = {
    VAR1: VAL1,
    VAR2: VAL2,
    VA31: VAL3,
};

I want it (either object or array) to be defined in a .env file. How can I do that?

like image 347
Trupti Avatar asked Sep 11 '20 11:09

Trupti


People also ask

Can you have an array in .env file?

short answer: yes, you can!

Can I use variables in .env file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.


Video Answer


2 Answers

value in .env value can only be string.

Simple workaround is to just store the env value as a comma separated value, like:

SOME_VAR=VAL1,VAL2,VAL3

and split it on your js file:

const someVar = process.env.SOME_VAR.split(",");

console.log(someVar); // [ 'VAL1', 'VAL2', 'VAL3' ]

Or use whatever delimiter you want.


If you want to store object, (unclean) workaround is to store JSON string as the env value, for example

OBJECT_VAL={ "VAR1": "VAL1", "VAR2": "VAL2", "VA31": "VAL3" }

and on your js code, you can parse the JSON:

const objectVal= JSON.parse(process.env.OBJECT_VAL);
console.log(objectVal); // { VAR1: 'VAL1', VAR2: 'VAL2', VA31: 'VAL3' }

I personally don't think storing JSON string inside .env is a good idea, so I would like to give my recommendation on better way to store .env value and use it on your js code.

1. Store env with normal string value, or delimiter separated value

For example:

ARRAY=VAL1,VAL2,VAL3

VAR1=VALl1
VAR2=VALl2
VAR3=VALl3

2. Make a js file to handle env variable

I will call it env.js, and on this file I will export object containing all env variable

module.exports = {
    array: process.env.ARRAY.split(","),
    object: {
       var1: process.env.VAR1,
       var2: process.env.VAR2,
       var3: process.env.VAR3,
    }
}

And on other file, you can just import env.js and call the env value

const env = require("path/to/env.js");

console.log(env.array); // [ 'VAL1', 'VAL2', 'VAL3' ]
console.log(env.object.var1); // "VAL1"

If your project often call process.env, this solution might make your code a bit cleaner since you don't need to call process. everytime you want to access your env variable.

like image 115
Owl Avatar answered Sep 22 '22 09:09

Owl


Two ways you can define an array in an .env file they are

  1. //In the .env file SOME_ARRAY = A , B ,C
  2. /In the .env file SOME_ARRAY = '["A","B","C"]' //In the code const convertedArray = JSON.parse(process.env.SOME_ARRAY)

In order to covert object ,unfortunately objects are not supported in .env files because they do not understand anything other than string , but we can keep an object in json format and they we can parse it in our folder where ever we want

MY_OBJECT = { "A":"One","B":"two","C":"three" } 
//we can access it in our code by follows 
const myObject = JSON.parse(process.env.MY_OBJECT)
//we can also access a certain property of that object like follows
const myObject = (JSON.parse(process.env.MY_OBJECT)).A   
like image 41
Hasibul Alam Rahi Avatar answered Sep 18 '22 09:09

Hasibul Alam Rahi