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

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

I tried this:

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

I am testing without process.env.MYKEY , but the console displays "This is installed."

+18
javascript environment-variables
source share
8 answers

This works fine in my Node.js project:

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

EDIT:

Please note that, as @Salketer mentioned, depending on needs, a false value in the above snippet will be considered false . In case a false value is considered a valid value. Use hasOwnProperty or check the value 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!'); } 
+23
source share

Why not check if the key exists in the environment variables?

 if ('MYKEY' in Object.keys(process.env)) console.log("It is set!"); else console.log("Not set!"); 
+3
source share

I use this snippet to find out if 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 stated in NodeJS 8 docs :

The process.env property returns an object containing the user environment. See environment (7) .

[...]

Assigning a property in process.env implicitly converts 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' 

Although, when the variable is not set in the environment, the corresponding key is generally absent in the process.env object, and the corresponding process.env property is undefined .

Here is another example (remember the quotation marks used in this 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 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' 

Additional Code Style Information

It is worth mentioning that the original question:

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

already contains the answer. To ensure that markup is added:

 How can I [check if] an [env var] [is set] in [Node.js]? 

Now literally translate this into JavaScript:

 if ('env_var' in process.env) 

Where:

  • [check if] = if
  • [env var] = 'env_var'
  • [is set] = in
  • [Node.js environment] = process.env

Simple and clear.

+2
source share

EDIT (deleted old wrong answer)

As Makskoryukov said, it should be:

 # in test.js if ("TEST_ENV" in process.env) { console.log("TRUE: " + process.env["TEST_ENV"]) } else { console.log("FALSE") } 

This was true when he passed the following test:

 $> node test.js FALSE $> export TEST_ENV="SOMETHING" $> node test.js TRUE: SOMETHING 

This also works when the variable is an empty string (tested in a new bash session / terminal window).

 $> node test.js FALSE $> export TEST_ENV="" $> node test.js TRUE: 
+1
source share

If you assign a value using the if statement, you can do it as follows

 var thisIsSet = 'asddas'; var newVariable = thisIsSet ||'otherValue' console.log(newVariable) 

Results in asddas

0
source share

The value (if it exists) will be a string, as indicated in the documentation :

 process.env.test = null; console.log(process.env.test); // => 'null' process.env.test = undefined; console.log(process.env.test); // => 'undefined' 

and you can return an empty string (what happened to me during the CI + GCP server)

I would create a function to clear 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' 
0
source share

This is a good way to check the environment variable.

 if (process.env.YOUR_ VARIABLE) { // If your variable is exist } 

Otherwise, if you want to check several environment variables, you can check this node module .

Host-envchecker

-one
source share
 let dotenv; try { dotenv = require('dotenv'); dotenv.config(); } catch(err) { console.log(err); // if vars are not available... } //... vars should be available at this point 
-2
source share

All Articles