Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I perform conditionals on environment variables in a node.js application

When I execute this node.js application…

if(process.env.ENV_ARG === "someValue") {
    console.log("conditional works...");
} else {
    console.log(`ENV_ARG => ${process.env.ENV_ARG}`);
}

with the following script…

  "scripts": {
    "dev": "SET ENV_ARG=someValue && node index.js"
  },

the output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ENV_ARG => someValue 

I can’t understand why the if statement evaluates to false.
When the contents of process.env.ENV_ARG are logged in the else-block, they should be accessible for the if statement above…at least I thought so…

Thanks in advance

>Solution :

It’s a matter of spacing. There is a space that gets included at the end of the environment variable.

One way to work around it is the following:

  "scripts": {
    "dev": "SET ENV_ARG=someValue&& node index.js"
  },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading