process.env

The Process object is a global variable that provides information that controls the current Node.js process. As an object, it is always available to Node.js applications, so require() is not required.

Process is actually a global variable that exists in NodeJS.

The process.env attribute returns an object containing information about the user’s environment.

Configuring environment Variables

(1) Windows configuration

Temporary configuration

Set NODE_ENV=production set NODE_ENV=production set variable name =% variable name %; Set path=%path%; C:\web; C: Tools # Delete the environment variable set NODE_ENV=Copy the code

Permanent configuration

Right-click (this computer) -> Properties (R) -> Advanced System Settings -> Environment Variables (N)...Copy the code

Echo $NODE_ENV echo $NODE_ENV echo $NODE_ENV

# if there is no add environment variable export NODE_ENV = # production environment variable additional value export path = $path: / home/download: / usr/local / # sometimes need to delete the environment variables unset NODE_ENV # At some point you need to display all environment variables envCopy the code

The NPM module cross-env can be configured with a single command on each platform

process-env

The installation

npm install --save-dev cross-env
Copy the code

Use is used in NPM scripts

{
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
  }
}
Copy the code

When set, you can use process.env.node_env in the webpack script

var env if (process.env.NODE_ENV === "testing") { env = require('.. /config/test.env') } else if (process.env.NODE_ENV === "uat") { env = require('.. /config/uat.env') } else if (process.env.NODE_ENV === "production") { env = require('.. /config/prod.env') } else if (process.env.NODE_ENV === "develop") { env = require('.. /config/develop.env') }Copy the code

Webpack.defineplugin sets global constants

DefinePlugin allows you to create a global constant that can be configured at compile time. This can be very useful when the development mode and the release mode are built to allow different behaviors. If logging is not performed in a development build but in a release build, global constants can be used to determine whether logging is performed. This is where the DefinePlugin comes in, setting it so you can forget the rules for developing and publishing builds.

 plugins: [
    new webpack.DefinePlugin({
      'process.env': env
    })
    ]
Copy the code

The use of global constants

Js /.vue is used in files within the project

const API_HOST = process.env.AUTOTEST_HOST
Copy the code