I’m working on an Express.js application in VS Code, and I’m trying to set the NODE_ENV environment variable to development. Here’s a simplified version of my Express route file (app.js):
const express = require("express");
const router = express.Router();
router.get("/", function(req, res) {
res.send("this is owner route");
});
console.log("NODE_ENV set to:", process.env.NODE_ENV); // Logging the environment
if (process.env.NODE_ENV === "development") {
console.log("you are in development environment");
}
router.post("/create", function(req, res) {
res.send("it is working");
});
module.exports = router;
Issue:
After setting NODE_ENV using set NODE_ENV=development command in PowerShell and then starting the server with npx nodemon, the console.log statement outputs NODE_ENV set to: undefined. However, I have verified that the environment variable is correctly set in my system settings.
Expected Outcome:
I expect the console.log statement to show NODE_ENV set to: development since I have explicitly set NODE_ENV to development.
Steps Taken:
Verified environment variable using echo $env:NODE_ENV in PowerShell, which correctly shows development.
Restarted VS Code and the terminal multiple times.
Checked my system’s environment variable settings to ensure correctness.
Question:
Why is the console.log statement showing undefined instead of development after setting NODE_ENV to development using VS Code?
Kartik Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.