Categories
JavaScript Answers

How to check if an environment variable is set in Node.js?

Spread the love

To check if an environment variable is set in Node.js, we use the in operator.

For instance, we write

if ("DEBUG" in process.env) {
  console.log(process.env.DEBUG);
} else {
  console.log("Env var IS NOT SET");
}

to check if the DEBUG property is in process.env with "DEBUG" in process.env to check if the DEBUG environment variable is set.

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 *