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!');
}
Answer from kucing_terbang on Stack OverflowThis 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!');
}
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.envproperty returns an object containing the user environment. See environ(7).[...]
Assigning a property on
process.envwill 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
You want the typeof operator. Specifically:
if (typeof variable !== 'undefined') {
// the variable is defined
}
The typeof operator will check if the variable is really undefined.
if (typeof variable === 'undefined') {
// variable is undefined
}
The typeof operator, unlike the other operators, doesn't throw a ReferenceError exception when used with an undeclared variable.
However, do note that typeof null will return "object". We have to be careful to avoid the mistake of initializing a variable to null. To be safe, this is what we could use instead:
if (typeof variable === 'undefined' || variable === null) {
// variable is undefined or null
}
For more info on using strict comparison === instead of simple equality ==, see:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
I'm using dotenv and storing all vars in a .env file for local development. When I use process.env I keep running into type issues all the time :(
I found some solutions online that say to extend the ProcessEnv interface, and although it seems to work I'm still running into issues.
For example when I use axios:
axios.get<User>(process.env.URL, ...)
I'm getting a compile time error of the form:
type 'string' | 'undefined' is not assignable to type 'string'
Any suggestions on how to properly set up types when using process.env?
Helper function that throws if the environment variable doesn't exist:
function getEnv(name) {
let val = process.env[name];
if ((val === undefined) || (val === null)) {
throw ("missing env var for " + name);
}
return val;
}
Then in code you just say;
pizza = getEnv("pizza");
Slightly improved original answer:
Comparing val with null is useless since val is either string or undefined. Plus when throwing, Error object should be used.
I have modified original code to look like:
function getEnv(name) {
const val = process.env[name];
if ((val === undefined)) {
throw Error("missing env var for " + name);
}
return val;
}