简体   繁体   中英

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

I'm accessing the NODE_ENV environment variable to turn on some debug features in a node.js server. It used to work like a charm, but now I'm facing some very weird stuff. Here'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 that possible? Am I facing a bug in node.js? If not, what am I doing wrong?

EDIT

Following Pointy's comment below, here's what I get if I change my initial log to console.log("[" + process.env.NODE_ENV + "]"); :

]development
nope
ok

So, a known issue maybe?

Looks like your environment variable has some funny characters, possibly due to the way it's being set outside of Node.js. You could try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM