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

user3812780 :

I would like to check if an environment variable is set in my Express JS server and perform different operations depending on whether or not it is set.

I've tried this:

if(process.env.MYKEY !== 'undefined'){
    console.log('It is set!');
} else {
    console.log('No set!');
}

I'm testing without the process.env.MYKEY but the console prints "It is set".

kucing_terbang :

This is working fine in my Node.js project:

if(process.env.MYKEY) { 
    console.log('It is set!'); 
}
else { 
    console.log('No set!'); 
}

EDIT:

Note that, As @Salketer mentioned, depends on the needs, falsy value will be considered as false in snippet above. In case a falsy value is considered as valid value. Use hasOwnProperty or checking the value once again inside the block.

> x = {a: ''}
{ a: '' }
> x.hasOwnProperty('a')
true

Or, feel free to use the in operator

if ("MYKEY" in process.env) {
    console.log('It is set!');
} else {
    console.log('No set!');
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to set and get environment variable in node js?

How can I check the environment variable in meteor

can i set node environment variable in Object format in Azure

How do I check if an environment variable is set in cmake

How can I set runtime variable for docker compose environment variable

How can i set scope authorization for a route in fastify and check user has access to that route or not with Node.js

How can I set the helm tiller namespace via an environment variable?

How can I set up the python environment variable on Windows 7?

How can I set .bash_profile environment variable in Dockerfile?

How can I set an environment variable which contains newline characters?

How can I set an environment variable as gulp task?

How can I set up an environment variable to work with TCC?

How can I consistently set an environment variable for a single program?

How can I set a runtime environment variable based on the value of another

How can I set the environment variable in request header for curl?

How can I set environment variable for another user account in Windows?

Can I check if Environment variable exist or not in Jenkinsfile

How do I set Linux environment variables so that pm2 can see them for node.js apps?

How does Node set its environment variable?

How to set environment variable in React JS..?

How do I set an environment variable?

How can I set the default Node.js version with nvm?

How to set environment variable in node.js process when deploying with github action

Can't set environment variable for debug node module

check if environment variable is already set

Set environment variable and run node.js server

NODE_ENV is showing undefined although i set environment variable

How can I set the environment based on route?

How can I unset an environment variable in Kubernetes?