process.env.NODE_ENV


In the Node environment, it is not an attribute in process.env, but is added to it by setting the system’s environment variable.

On Windows, we type node on the command line, enter node mode, and find that the process.env object has no NODE_ENV attribute.

1. Creation mode

  • Use of the cross-env plug-in to set this variable across platforms
"scripts": {
    "dev""cross-env NODE_ENV=production webpack-dev-server"
  }
Copy the code
  • The DefinePlugin plugin sets javascript global variables
new Webpack.DefinePlugin({
     'process.env': {NODE_ENV: json.stringify (process.env.node_env) // To char the return value}})Copy the code

2. Usage

  • It can be used in the js file process.env.node_env.

3. Use

  • Distinguish parameter configurations in different environments, including management and configuration parameters dependent on development and production environments:
  1. DevServer parameters;
  2. Loader and Plugin plug-ins;
  3. SourceMap, etc.

4. Previous misunderstandings

  1. Using DefinePlugin to set the value of NODE_ENV in a system environment variable is a two-step process: set (cross-env) and fetch (DefinePlugin).
  2. Cross-env was designed to address the issue of Windows setting environment variables incorrectly across platforms (one of the pain points).