Understanding process.env.NODE_ENV

Dec 5, 2021 · 1 min read · 176 Words · -Views -Comments

This variable is commonly encountered in frontend builds or Node.js development, but I never systematically understood its origins or impact. Here’s a summary.

  1. NODE_ENV is an environment variable.
  2. Installing Node.js or Webpack does not set this variable; we usually need to set it ourselves. Before setting it, printing it will show undefined.
  3. This environment variable was introduced by Express and has now become a conventional practice.
  4. Whether setting NODE_ENV to development/production or other values makes a difference depends on whether the tools used rely on this value. For example, in webpack, we sometimes selectively enable certain plugin processing based on NODE_ENV, such as hash fingerprint generation.

Modifying the Value in Projects

For CI builds, we often set this value at the Docker container level. For local development, where we need flexibility between projects, project-level control is better.

$ cross-env NODE_ENV=production node testaaa.js

Environment Variables

When I was doing Java development, I often had to configure Path and ClassPath, which are system environment variables.

Viewing Environment Variables on macOS

$ env

$ echo $NODE_ENV

Final Thoughts

Done.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover