How to set process.env.NODE_ENV production environment mode

How to set process.env.NODE_ENV production environment mode

Before I start, let me emphasize that process.env.NODE_ENV has only two states by default, development and production. Development refers to local development, namely the localhost environment (local development), and production represents services published on any service (whether it is dat, uat or production environment). Node does not know whether your service is a test or a formal one unless you specify it manually. It is usually considered to be an online environment. So we can think of development as representing the local development environment, and production as representing the online environment (including dat, uat, and production environments, etc.)

Why do I emphasize this point?

Because some people actually use development in process.env.NODE_ENV to represent online test environments such as dat and uat, it is particularly emphasized that development represents the local development environment.

Recently, the system was connected to the company's single sign-on. In order to jump back to the system's main page after a successful login, I applied for a domain name for testing and sandbox (previously I logged in directly with the IP address). The host is configured for local development. After connecting, I found a very troublesome point that the jump address passed for single sign-on needs to be modified every time. It needs to be written as the development domain name during development, changed to the test domain name during testing, changed to the domain name for jumping to the sandbox when entering the sandbox, and changed to the online domain name when going online. Especially during the testing phase, switching back and forth between development and testing is extremely annoying.
So I wanted to write it as a configuration file and load different configurations according to different environments, so that I don’t have to change it back and forth. At this time, process.env jumped into my mind.

A look at process.env

The process object is a global variable that provides information about the current node.js and controls the relevant processes of the current node.js. Because it is a global variable, it is always available to the node application without the need to require().

Since process is an object, env is naturally one of its attributes, which returns an object containing user environment information. After entering node in the terminal, you can see the printed information by entering process.env.

The protagonist appears process.env.NODE_ENV

NODE_ENV is not an existing property of the process.env object, so how was it added?

Let’s take an example:

In package.json, it is as follows:

{
  "name": "yun-nobile",
  "version": "2.0.0",
  "description": "Taibao standard mobile product 2.0, based on vue",
  "main": "yunprod.js",
  "scripts": {
    "build": "cross-env NODE_ENV=production node yunprod.js build",
    "dev": "node yunprod.js dev"
  }
  ...
}  

When we execute the npm run build script command, cross-env NODE_ENV=production node yunprod.js build will be executed, and NODE_ENV will be set to production, so process.env.NODE_ENV will be set to production.

So process.env.NODE_ENV is a global environment variable added when we execute the script command.

process.env.NODE_ENV is used to determine the current development stage. Generally, the production stage is set to production, the development stage is set to develop, and then process.env.NODE_ENV is read in the script.
When running the script, you can change the environment variables like this, add the command in the scripts of the package.json file:

NODE_ENV=production node build.js

However, this command will report an error when the students who use Windows download the code, because the settings on Windows are different

set NODE_ENV=production node build.js

But different settings on different computers will definitely not work, this is when cross-env comes to the rescue.
cross-env can set and use environment variables across platforms

npm install --save-dev cross-env

Next we can set it up through cross-env

cross-env NODE_ENV=production node build.js

After this setting, we can use process.env.NODE_ENV in scripts, but not in modules. To use it directly in modules, we need some configuration

In webpack 4+ you can use the mode option:

module.exports = {
  mode: 'production'
}

But in webpack 3 and lower, you need to use DefinePlugin:

var webpack = require('webpack')

module.exports = {
  // ...
  plugins: [
    // ...
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('production')
    })
  ]
}

This way you can use it directly
Now we need to configure different URLs in the module based on environment variables

let url = '';
 if (process.env.NODE_ENV === 'testing') {
   url = 'http://my.test.cn';
 } else if (process.env.alpord === 'alpord') {
   url = 'http://my.alpord.cn';
 } else if (process.env.NODE_ENV === 'production') {
   url = 'http://my.product.cn';
 } else {
   url = 'http://my.develop.cn';
 }

or

let url = '';
process.env.NODE_ENV === 'production'?url = 'http://my.product.cn':url = 'http://my.test.cn';

This is the end of this article on how to set process.env.NODE_ENV production environment mode. For more information about setting process.env.NODE_ENV production environment mode, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • In-depth understanding of webpack process.env.NODE_ENV configuration

<<:  Docker solves the problem that the terminal cannot input Chinese

>>:  MySQL 8.0.21.0 Community Edition Installation Tutorial (Detailed Illustrations)

Recommend

MySQL Database Basics SQL Window Function Example Analysis Tutorial

Table of contents Introduction Introduction Aggre...

Brief analysis of centos 7 mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar

Baidu Cloud Disk: Link: https://pan.baidu.com/s/1...

Detailed explanation of MySQL Strict Mode knowledge points

I. Strict Mode Explanation According to the restr...

How to deploy LNMP & phpMyAdmin in docker

Environmental preparation: Deploy lnmp on a host ...

Graphical explanation of the underlying principle of JavaScript scope chain

Table of contents Preface Scope 1. What is scope?...

Refs and Ref Details in Vue3

The editor also shares with you the corresponding...

How to delete folders, files, and decompress commands on Linux servers

1. Delete folders Example: rm -rf /usr/java The /...

js canvas realizes circular water animation

This article example shares the specific code of ...

Detailed process of modifying hostname after Docker creates a container

There is a medicine for regret in the world, as l...

Solution to MySQL root password error number 1045

Stop MySQL Service Windows can right-click My Com...

MYSQL 5.6 Deployment and monitoring of slave replication

MYSQL 5.6 Deployment and monitoring of slave repl...

Detailed discussion of the differences between loops in JavaScript

Table of contents Preface Enumerable properties I...

7 cool dynamic website designs for inspiration

In the field of design, there are different desig...