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

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!');
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)