无法正确访问NODE_ENV环境变量,这是node.js的错误吗?

时间:2022-01-10 23:08:42

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:

我正在访问NODE_ENV环境变量以打开node.js服务器中的一些调试功能。它过去像魅力一样工作,但现在我面临一些非常奇怪的东西。这就是我正在做的事情:

// 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?

怎么可能?我是否面临node.js中的错误?如果没有,我做错了什么?

EDIT

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

按照下面的Pointy的评论,如果我将初始日志更改为console.log(“[”+ process.env.NODE_ENV +“]”),我会得到以下内容:

]development
nope
ok

So, a known issue maybe?

那么,一个已知问题可能呢?

1 个解决方案

#1


11  

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:

看起来您的环境变量有一些有趣的字符,可能是由于它在Node.js之外设置的方式。你可以试试这个:

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

#1


11  

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:

看起来您的环境变量有一些有趣的字符,可能是由于它在Node.js之外设置的方式。你可以试试这个:

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