Skip to content
Advertisement

Defining an array as an environment variable in node.js

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement