← Back to Node.js, page 5

nodejs-0099 · Easy fresher

What does NODE_ENV=production change, and why do dependencies and devDependencies matter for deployment?

Node itself ignores NODE_ENV. Frameworks and libraries read it to switch behaviour: Express caches views and hides stack traces, many libraries skip development checks and logging. npm install also skips devDependencies when NODE_ENV is production (or with --omit=dev), which keeps servers small. Set it on servers and keep staging and production the same, using a separate variable for the environment name.

Full answer

NODE_ENV is only a convention. The runtime does nothing with it, but a lot of code checks process.env.NODE_ENV.

What changes in production mode (library dependent):

  • Express: caches compiled views, produces less verbose error output (no stack traces in the default error handler), and uses other optimisations.
  • React, Vue and similar libraries: remove development warnings and checks, which makes them smaller and faster. Bundlers replace the variable at build time.
  • Logging and debugging helpers may reduce output.
  • Some libraries enable stricter security defaults or turn on caching.

Dependencies:

  • dependencies are installed always. devDependencies (tests, linters, build tools) are skipped by npm install --omit=dev, and by npm ci --omit=dev. Older npm used --production.
  • Put only what the app needs at run time in dependencies. A build tool in dependencies makes images larger and widens the attack surface.
  • Build steps that need dev tools happen before the omit step (multi-stage builds or a CI build job).

Advice:

  • Set NODE_ENV=production on every deployed environment, including staging, so staging behaves like production. Do not invent values such as 'staging' or 'qa' for NODE_ENV because libraries only know 'production' and 'development'. Use a separate variable (APP_ENV) for environment names.
  • Do not write your own logic that depends on many NODE_ENV values. Prefer explicit configuration flags.
  • Never ship code that works only when NODE_ENV is not set.
  • Test the app once with NODE_ENV=production locally, because some bugs appear only there.
javascript
const { spawnSync } = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');

// A small Express app whose route throws an error. The default error handler decides what to show.
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'node-env-'));
const expressPath = require.resolve('express');
fs.writeFileSync(path.join(dir, 'app.js'), `
const express = require(${JSON.stringify(expressPath)});
const app = express();
app.get('/crash', () => { throw new Error('database password is wrong'); });
const server = app.listen(0, '127.0.0.1', async () => {
  const res = await fetch('http://127.0.0.1:' + server.address().port + '/crash');
  const body = await res.text();
  console.log('NODE_ENV =', process.env.NODE_ENV, '| status', res.status,
    '| response shows the stack trace:', body.includes(' at '),
    '| response shows the message:', body.includes('database password'));
  server.close();
});
`);

for (const env of ['development', 'production']) {
  const run = spawnSync(process.execPath, [path.join(dir, 'app.js')], { env: { ...process.env, NODE_ENV: env }, encoding: 'utf8' });
  console.log(run.stdout.trim());
}

fs.rmSync(dir, { recursive: true });

The same route is served twice with a different NODE_ENV. In production the default error handler leaves out the stack trace and the error message. Install first with npm install express (run here with express 5.3.0). Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): NODE_ENV = development | status 500 | response shows the stack trace: true | response shows the message: true NODE_ENV = production | status 500 | response shows the stack trace: false | response shows the message: false

Key points

  • Node ignores NODE_ENV, libraries read it.
  • Express hides stack traces and caches views in production.
  • npm ci --omit=dev skips devDependencies.
  • Use APP_ENV for custom environment names.

Common mistakes

  • Setting NODE_ENV to custom names like staging.
  • Leaving build tools in dependencies.
  • Running production with NODE_ENV unset and getting slower, noisier behaviour.
  • Using NODE_ENV checks scattered in business code.

Follow-ups

  • What is the difference between npm install --omit=dev and npm ci --omit=dev?
  • How does a bundler use NODE_ENV?
  • How do you make the error handler safe in your own app?

Tips

  • Begin with 'Node itself ignores it'.
  • Give the Express example.
  • Say where custom environment names belong.