Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to check if an environment variable is set in Node.js.

In this article, we’ll look at how to check if an environment variable is set in Node.js.

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

To check if an environment variable is set in Node.js, we can check if the environment variable exists in process.env.

For instance, we write

if (process.env.MYKEY) {
  console.log("set");
} else {
  console.log("not set");
}

to check if the MYKEY environment variable is set by checking is process.env.MYKEY is truthy.

Conclusion

To check if an environment variable is set in Node.js, we can check if the environment variable exists in process.env.

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 *