Environments
An environment describes where your Express app is running: on your laptop during development, on a test server, or in production serving real users. Each one needs slightly different behaviour. Development wants detailed error messages and request logging, while production wants speed, security and quiet logs. Node exposes the current environment through the NODE_ENV variable.
Reading the environment in Node
Environment variables are values set outside your code and passed to the process by the operating system or hosting platform. Node makes them available on process.env. By convention NODE_ENV holds development, test or production. If nothing sets it, it is simply undefined.
Express itself has a helper: app.get("env") returns process.env.NODE_ENV, or "development" if it is not set. That default is convenient on your machine but dangerous on a server, as we will see.
const express = require("express");
const morgan = require("morgan");
const app = express();
const env = app.get("env");
if (env === "development") {
app.use(morgan("dev")); // log every request in colour
console.log("Request logging enabled");
}
app.get("/api/health", (req, res) => {
res.json({ status: "ok", env });
});
app.use((err, req, res, next) => {
const body = { message: "Something went wrong" };
if (env !== "production") body.stack = err.stack;
res.status(500).json(body);
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on ${port}`));
Setting NODE_ENV
On macOS or Linux you can set it for one command: NODE_ENV=production node index.js. In Windows PowerShell, run $env:NODE_ENV="production" first and then start the app. Because these differ, many projects use the cross-env package in their npm scripts so the same command works everywhere. Hosting platforms usually provide a settings page for environment variables, and Docker uses ENV lines or the -e flag.
What changes in production mode
Setting NODE_ENV=production is not just a label. Express caches view templates and produces shorter error output. Many libraries also check the variable and skip expensive debugging work. You should add your own differences on top:
- Turn off verbose request logging, or send logs to a file or service.
- Hide stack traces from API responses, since they reveal file paths and code structure.
- Enable security middleware such as
helmetand strict CORS settings. - Connect to the production database rather than a local one.
The forgotten variable
The classic mistake is deploying without setting NODE_ENV. Because app.get("env") falls back to development, the live server happily prints stack traces to every user who triggers an error and runs debug-only middleware. Always set it explicitly on every server, and consider logging the value at startup so you can confirm it.
A related mistake is scattering checks like if (process.env.NODE_ENV === "production") through dozens of files. It works, but it is hard to see all the differences in one place. Gathering environment-dependent values into a single configuration module, the subject that usually follows this one, keeps things tidy.
Test environment
Running tests with NODE_ENV=test lets you point the app at a throwaway database and switch off noisy logging, so test output stays readable and real data is never touched.
Takeaway: read the environment from NODE_ENV, always set it on servers, enable logging and stack traces only outside production, and keep environment-specific choices in one place.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.