Detailed description of the environment variable process. env in Node. js, node. jsprocess. env

Source: Internet
Author: User

Detailed description of the environment variable process. env in Node. js, node. jsprocess. env

Preface

We have been working with O & m gg to deploy the project in the last two days. One problem is our dev, uat, and product environment.

Because it is the frontend and backend separation, the domain name of the backend interface will be switched during development and deployment. After one afternoon, I checked all kinds of information to figure out the Node environment variable process. env.

Here is a record for solving the problem. I hope this will be helpful to the person I don't understand. Let's take a look at the detailed introduction.

Node environment variable

First of all, when we develop a single-page application of react and vue, we believe that process. env in the configuration file is not eye-catching.

Cannot remember? Black question No. .jpg.

These are the following things.

Literally, This is the env attribute. The configuration varies depending on the development and production environments.

Okay. Now let's take a look at what the so-called process is.

Document: http://nodejs.cn/api/process.html

Official explanation: the process object is a global (global variable) that provides relevant information to control the current Node. js process. As an object, it is always available for Node. js applications, so you do not need to use require ().

Process is actually a global variable in nodejs.

Then, we can get something interesting through this so-called process.

However, today we mainly talk about process. env.

Process. env

What is this?

Official: The process. env attribute returns an object containing user environment information.

Document: http://nodejs.cn/api/process.html#process_process_env

Oh, it turns out that a property can return some information about the environment where the project is running.

What's the purpose?

Obviously, in this scenario, we can tag the server. In this way, we can perform some configuration Processing Based on different environments. For example, you can enable sourceMap and switch the domain names of backend interfaces.

You are in the dev environment. It is in The uat environment. She is in the product environment.

How to configure Environment Variables

The following describes how to configure environment variables for each environment.

Windows Configuration

Temporary Configuration

You can directly configure the environment in cmd to view the environment variables, add the environment variables, and delete the environment variables.

# The environment variables commonly used in node are NODE_ENV, first, check whether there is set NODE_ENV # Add the environment variable set NODE_ENV = production # Add the environment variable value set variable name = % variable name %; variable content set path = % path %; c: \ web; C: \ Tools # in some cases, you must delete the environment variable set NODE_ENV =

Permanent Configuration

Right-click (this computer)-> Properties (R)-> advanced system settings-> environment variable (N )...

Linux Configuration

Temporary

View environment variables, add environment variables, and delete Environment Variables

# The environment variables commonly used in node are NODE_ENV, first, check whether the echo $ NODE_ENV export NODE_ENV = production # Add the environment variable export path = $ path:/home/download: /usr/local/# Delete the environment variable unset NODE_ENV in some cases # display all environment variables env in some cases

Permanent

Open the configuration file

# All users have activated vim/etc/profile # The current user has activated vim ~ /. Bash_profile

Add the following statement at the end of the file to set or modify the environment variables.

# Add the following environmental variable export path = $ path:/home/download:/usr/local/export NODE_ENV = product at the end of the file

After the modification is complete, run the following statement to reload the system.

# Modify the/etc/profile file and then source/etc/profile # modify ~ /. Source ~ after the bash_profile File ~ /. Bash_profile

Solve the Problem of backend interface transformation caused by the Environment

After figuring out this problem, we can set different NODE_ENV on machines in different environments. Of course, this field is not necessarily.
You can also switch to other NODE_ENV_NIZUISHUAI and so on. It is customized.

Solution

1. Modify the backend address configuration in the code

It's easy to use.process.env.NODE_ENVThis field is used to determine. (Process is the global attribute of node and can be used directly)

2. Set environment variables on linux

export NODE_ENV=dev

Then you can start the project happily.

Last

Because I now use this project to React server rendering. Therefore, the backend request is not forwarded to nginx for processing.
For common single-page applications, nginx is generally used for request forwarding.

This article is faster. Haha, if there are any mistakes in this article, please give me some advice and I will learn it.

In addition, if this small article can help you, I may wish to give you some praise. This will be a major motivation for me to continue.

Thank you ~~

References

  • Environment Variable-JasperXu blog: http://sorex.cnblogs.com/p/6200940.html
  • How do I view and set the NODE_ENV value? : Http://cnodejs.org/topic/587dc8a62967eeb01aafe87b
  • Node. js process Module: http://www.css88.com/archives/4548

Note:[How to configure environment variables] This section refers to the environment variables directly referenced by JasperXu's blog.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.