Can't access the NODE_ENV environment variable correctly, is it a bug with node.js?

I am referring to the NODE_ENV environment variable to enable some debugging features on the node.js server. It worked like a charm, but now I come across some very strange things. That's what I'm doing:

// check if the env var is OK console.log(process.env.NODE_ENV); // WTF??? if (process.env.NODE_ENV == "development") { console.log("ok"); } else { console.log("nope"); } // sanity check var str = "development"; if (str == "development") { console.log("ok"); } else { console.log("nope"); } 

And here is what I get:

 development nope ok 

How is this possible? Am I encountering an error in node.js? If not, what am I doing wrong?

EDIT

The following noteworthy comment below is what I get if I change my original log to console.log("[" + process.env.NODE_ENV + "]"); :

 ]development nope ok 

So a known problem?

+7
source share
1 answer

It looks like your environment variable has some funny characters, possibly due to the fact that it is set outside of Node.js. You can try the following:

 if (process.env.NODE_ENV.replace(/\W/g, '') == 'development') { console.log('ok'); } 
+10
source

All Articles