0

I am trying to store some array in .env files but when I access the particular value I got typeof string and I have use forEach to process my next step. in the .env file, email is stored like this

EMAILS = JSON.stringify(['[email protected]','[email protected]'])

I have used in this way also

EMAILS = ['[email protected]','[email protected]']

and in this way a

EMAILS = "['[email protected]','[email protected]']"

I want to make that array which is showing type of string into a normal array.

2
  • 1
    .env is not a javascript file, it is just a txt file. if you want to store multiple values, you can do the following EMAILS = [email protected];[email protected] and parse that string when you are reading the variable const emails = process.env.EMAILS.split(';') Commented May 15, 2020 at 14:28
  • It's not very clear. Do you mean to say while storing you're using JSON.stringify and you'd like to parse it back into the array while retrieving? Commented May 15, 2020 at 14:29

1 Answer 1

1

It would be better not to store serialised JSON inside an .env file. Instead, store your "array" of items as a delimited list, for example using commas to separate the individual values. Like this:

# .env
[email protected],[email protected]
// javascript
const emails = env.EMAILS.split(",")

However, if you must store a serialised array, then you can parse it into JS by using JSON.parse:

# env
EMAILS=["[email protected]","[email protected]"]
// javascript
const emails = JSON.parse(env.EMAILS)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.