How can I check if an environment variable is set in Node.js?
Solution 1
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!');
}
Solution 2
I use this snippet to find out whether the environment variable is set
if ('DEBUG' in process.env) {
console.log("Env var is set:", process.env.DEBUG)
} else {
console.log("Env var IS NOT SET")
}
Theoretical Notes
As mentioned in the NodeJS 8 docs:
The
process.env
property returns an object containing the user environment. See environ(7).[...]
Assigning a property on
process.env
will implicitly convert the value to a string.process.env.test = null console.log(process.env.test); // => 'null' process.env.test = undefined; console.log(process.env.test); // => 'undefined'
Though, when the variable isn't set in the environment, the appropriate key is not present in the process.env
object at all and the corresponding property of the process.env
is undefined
.
Here is another one example (be aware of quotes used in the example):
console.log(process.env.asdf, typeof process.env.asdf)
// => undefined 'undefined'
console.log('asdf' in process.env)
// => false
// after touching (getting the value) the undefined var
// is still not present:
console.log(process.env.asdf)
// => undefined
// let's set the value of the env-variable
process.env.asdf = undefined
console.log(process.env.asdf)
// => 'undefined'
process.env.asdf = 123
console.log(process.env.asdf)
// => '123'
A side-note about the code style
I moved this awkward and weird part of the answer away from StackOverflow: it is here
Solution 3
Why not check whether the key exists in the environment variables?
if ('MYKEY' in Object.keys(process.env))
console.log("It is set!");
else
console.log("Not set!");
Solution 4
As the value (if exist) will be a string, as mentioned in the documentation:
process.env.test = null;
console.log(process.env.test);
// => 'null'
process.env.test = undefined;
console.log(process.env.test);
// => 'undefined'
and empty string can be returned (that happened to me in CI process + GCP server),
I would create a function to clean the values from process.env
:
function clean(value) {
const FALSY_VALUES = ['', 'null', 'false', 'undefined'];
if (!value || FALSY_VALUES.includes(value)) {
return undefined;
}
return value;
}
const env = {
isProduction: proces.env.NODE_ENV === 'production',
isTest: proces.env.NODE_ENV === 'test',
isDev: proces.env.NODE_ENV === 'development',
MYKEY: clean(process.env.MYKEY),
};
// Read an environment variable, which is validated and cleaned
env.MYKEY // -> 'custom values'
// Some shortcuts (boolean) properties for checking its value:
env.isProduction // true if NODE_ENV === 'production'
env.isTest // true if NODE_ENV === 'test'
env.isDev // true if NODE_ENV === 'development'
Solution 5
I do practice to use built-in Node.js assert library.
const assert = require('assert');
assert(process.env.MY_VARIABLE, 'MY_VARIABLE is missing');
// or if you need to check some value
assert(process.env.MY_VARIABLE.length > 1, 'MY_VARIABLE should have length greater then 1');
I use to add this validation on top of the index.js, and keep it up to date with the code requirements. This way is also easy to check which variables are required for the project, if for some reason .env.example is not in the code.
user3812780
Updated on July 09, 2022Comments
-
user3812780 almost 2 years
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".