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?
NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production).
You cannot override NODE_ENV manually. This prevents developers from accidentally deploying a slow development build to production.
You really do not need to set up your own environment to start learning Node. js. Reason is very simple, we already have set up Node.
The dotenv package for handling environment variables is the most popular option in the Node. js community. You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables.
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With