process.env

Process. env is an API provided by Nodejs that returns an object containing information about the user environment. If we set an environment variable to Nodejs and mount it on the object returned by process.env, we can make appropriate environment judgments in our code.

Set up process. The env

This is usually done by setting package.json, but there are differences between Windows and Mac systems.

Windows system

// package.json{..."scripts": {
    "dev": "set NODE_ENV=development webpack-dev-server"."build": "set NODE_ENV=production xxx"}}Copy the code

Mac system

// package.json{..."scripts": {
    "dev": "export NODE_ENV=development webpack-dev-server "."build": "export NODE_ENV=production xxx"}}Copy the code

They all have the same syntax, which can cause problems in the opposite environment, hence cross-env. Cross-env is a third-party package for setting environment variables across platforms, allowing you to easily set environment variables across multiple platforms with a single command configured. First install

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

then

// package.json{..."scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server "."build": "cross-env NODE_ENV=production xxx"}}Copy the code

use

It can be used globally

    //webpack.config.js
    const isProd = process.env.NODE_ENV === 'production';
Copy the code