Defining an array as an environment variable in node.js
Asked Answered
U

6

141

I have an array that I pull data from.

festivals = ['bonnaroo', 'lollapalooza', 'coachella']

Since I'm using heroku, it may be better to replace it with an environment variable, but I'm not sure how to do that.

Is using a JSON string as an environment variable the way to go?

Uncertainty answered 22/7, 2015 at 0:56 Comment(0)
P
196

In this scenario, it doesn't sound like an env var is the way to go.

Usually, you'll want to use environment variables to give your application information about its environment or to customize its behavior: which database to connect to, which auth tokens to use, how many workers to fork, whether or not to cache rendered views, etc.

Your example looks more like a model, so something like a database is probably a better fit.

That said, there's no context around what your app does or how it uses festivals, so if it does turn out that you should use an env var, then you have several options. The simplest is probably to just use a space or comma-delimited string:

heroku config:set FESTIVALS="bonnaroo lollapalooza coachella"

then:

var festivals = process.env.FESTIVALS.split(' ');

disclosure: I'm the Node.js Platform Owner at Heroku

Politician answered 22/7, 2015 at 1:44 Comment(0)
G
112

Your example looks more of an enumeration than a config array. I'd highly recommend using a model to save it.

In case you are referring to the above array just as an example and are more curious about how can arrays be stored in an env file -

Short answer: You cannot.

Long answer: .env variables are strings So something like

BOOLEAN = true

will be treated as

BOOLEAN = "true"

and so will

FESTIVALS = ['bonnaroo', 'lollapalooza', 'coachella'] 

be treated as

FESTIVALS = "['bonnaroo', 'lollapalooza', 'coachella']"

Solution:

You can save the array as a delimited string in .env

FESTIVALS = "bonnaroo, lollapalooza, coachella"

In your js file you can convert it to an array using

var festivals = process.env.FESTIVALS.split(", ");

The result will be

['bonnaroo', 'lollapalooza', 'coachella']
Ghislainegholston answered 7/2, 2019 at 7:19 Comment(3)
Note the space after the comma in the split(", "). I missed it at first, and my includes() string matches failed =/ – Endoderm
improvement: .split(",").trim() – Dismal
You mean: .split(",").map(i => i.trim()) – Address
C
83

Use JSON Format ( Best Answer πŸ’ͺπŸŽƒ )

Define :

ANY_LIST = ["A", "B", "C"]

Parse :

const LIST = JSON.parse(process.env.ANY_LIST);

Use :

console.log(LIST[2]);    // prints: "C"

console.log(Array.isArray(LIST));    // prints: true
Classis answered 12/6, 2020 at 19:53 Comment(0)
I
22

short answer: yes, you can!

Although a .env variable is string, you can parse it into an array

2 ways to do it are:

1.JSON.parse()

YOUR_ENV = ["A", "B", "C"] # in your .env file
const envs = JSON.parse(process.env.YOUR_ENV); // in your app file

2. split()

YOUR_ENV = "A, B, C"
const envs = process.env.YOUR_ENV.split(", ");
Interleaf answered 20/5, 2022 at 7:10 Comment(0)
R
8

It probably depends on your data. For example, if none of the values will ever contain commas, you could just make it a comma-separated list and then split on a comma (e.g. starting your app with FOO=bar,baz,quux node myapp.js then doing var foo = process.env.FOO.split(',') in myapp.js).

Otherwise if your input values can be more complex, JSON will probably be the easiest to work with.

Rudd answered 22/7, 2015 at 1:44 Comment(1)
agree to use , instead of ' ' – Vinny
R
0

Note that it can also be useful to enable multiline mode when you need a long list:

const env = require('dotenv').config({ multiline: 'line-breaks' })

https://github.com/motdotla/dotenv/commit/0a9332ff70f5121c1221d5c32839a759c60cb879


Full example:

FESTIVALS="bonnaroo
lollapalooza
coachella"
const env = require('dotenv').config({ multiline: 'line-breaks' });
const festivals = env.FESTIVALS.split('\n');
Religiosity answered 13/12, 2022 at 12:55 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.