Categories
JavaScript Answers

How to define an array as an environment variable in Node.js?

Spread the love

To define an array as an environment variable in Node.js, we can define it as a JSON array.

For instance, we write

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

to add the ANY_LIST environment variable to our .env file.

Then we write

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

to get the ANY_LIST environment variable with process.env.ANY_LIST.

And we parse it into an array with JSON.parse.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *