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
.