Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the process.env.NODE_ENV production environment mode

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to set the process.env.NODE_ENV production environment model, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Before we start, I would like to emphasize that by default, process.env.NODE_ENV has only two states: development and production,development refer to local development, that is, localhost environment (local development), while production represents a service published on any service (whether it is dat, uat or production environment). Node does not know whether your service is testing or formal, unless you specify it manually. It is generally thought of as an online environment. So it can be thought that development represents the local development environment, and production represents the online environment (including dat, uat, production environment, etc.)

Why should I emphasize this point?

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

Recently, the system connected to the company's single sign-on, in order to successfully log in and then jump back to the main page of the system, and applied for test and sandbox domain names (previously logged in directly with IP), and the local development is configured with host. After connecting, I found that a very troublesome point is that the jump address passed to single sign-on has to be modified every time, written as the developed domain name during development, changed to the tested domain name during testing, changed to jump sandbox domain name when entering the sandbox, and changed to online domain name when it is online. Especially in the testing phase, it is tiresome to switch development tests back and forth.

So I want 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.

Pick a process.env.

The process object is a global variable that provides information about the current node.js and the processes that control the current node.js. Because it is a global variable, it is always available for node applications without the need for require ().

Now that process is an object, env is naturally a property of it, which returns the object containing the user's environment information. After entering node at the terminal, you can see the printed information after entering process.env.

The protagonist appears in process.env.NODE_ENV

NODE_ENV is not an original property on the process.env object, so how is it added?

Let me give you an example:

In package.json, it is as follows:

{"name": "yun-nobile", "version": "2.0.0", "description": "Taibao Standard Mobile products 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, we execute cross-env NODE_ENV=production node yunprod.js build and set NODE_ENV to production, so process.env.NODE_ENV is set to production.

So process.env.NODE_ENV is a global environment variable that we add when we execute script commands.

Process.env.NODE_ENV is used to determine the current stage of development. The general production phase is set to production, the development phase is set to develop, and then the process.env.NODE_ENV is read in the script.

When you run the script, you can change the environment variables by adding commands to the scripts in the package.json file:

NODE_ENV=production node build.js

But this command is wrong when the students using Windows pull down the code, because the way it is set up above Windows is different.

Set NODE_ENV=production node build.js

But different settings on different computers certainly won't work, and cross-env came to the rescue at this time.

Cross-env can set and use environment variables across platforms

Npm install-save-dev cross-env

Then we can set it through cross-env.

Cross-env NODE_ENV=production node build.js

After setting this up, we can use process.env.NODE_ENV in the script, but not in the module. To use it directly in the module, we need some configuration.

In webpack 4 +, you can use the mode option:

Module.exports = {mode: 'production'}

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

Var webpack = require ('webpack') module.exports = {/ /... Plugins: [/ /... New webpack.DefinePlugin ({'process.env.NODE_ENV': JSON.stringify (' production')})]}

So you can use it directly.

Now we need to configure different url according to the environment variables in the module.

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'; Thank you for reading this article carefully. I hope the article "how to set up the process.env.NODE_ENV production environment mode" shared by the editor will be helpful to everyone. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report