preface
It’s been a long time since an external technical document was released. I looked at the nuggets’ record and the last technical document was written in November. Why write this article? Generally, we need to configure environment variables to build the development environment. Some people don’t know what environment variables are, and even if they are configured, they don’t know what they are configured, and they don’t know how the system uses environment variables.
What are environment variables
Let’s talk about variables first. Variables are derived from mathematics. In computer language, an abstract concept that can store the results of a calculation or represent a value. Those of you who believe in programming know what a variable is. Now let’s look at a piece of JS code.
var age = 11;
var name = 'james';
console.log(`${name} is ${age} years old`);
Copy the code
In this code, we declare two variables, one is age, one is name, they are very simple to represent a value, age represents the age, name represents the name. We can use variables after declaring them, such as the one printed in console.log. So what exactly are environment variables? The difference is that age and name are language level variables, whereas environment variables are computer system level variables, and basically any programming language can get the environment variables of a computer, and you can do a lot of things with environment variables.
How do programming languages get environment variables
This is an introduction to getting environment variables in a programming language. This is done using the programming language nodeJS. Nodejs has a built-in module called process, and process has an attribute called env, which we can use to retrieve local environment variables. Env is short for environment. The values of environment variables are stored as strings, and there is an unwritten convention that they are all uppercase letters, with multiple words separated by _.
var HOME = process.env['HOME'];
var PATH = process.env['PATH'];
console.log(HOME);
console.log(PATH);
Copy the code
Run the above code to print the HOME and PATH environment variables to the console. Other popular programming languages also have easy access to environment variables, but they’re just written differently.
How do I view environment variables
There are many differences between Windows and Mac viewing local environment variables.
Windows
On the System Properties screen, select Advanced. On the Advanced screen, select Environment Variables. The variable configuration screen is displayed.
Next comes the panel for environment variables. There are two types of environment variables, user variables and system variables.
- User variables: Essentially environment variables, but only for the current user. For example, we all know that a computer can have multiple users, and the user variable changes as the computer logs in to other accounts.
- System variables: Essentially environment variables that take effect no matter which user you log in to. Therefore, define variables that are not relevant to the user.
As you can see, this panel also supports new, edit, delete functions, but add, delete, change and check everything. 🐂 🍺
Mac
To list all environment variables, type env directly on the terminal. Windows a lot of configuration are made visual, the Use of the Mac command line will be more, it should be the default terminal has a lot of Linux commands, this point is very convenient to use.
$ env
SHELL=/bin/zsh
HOME=/Users/wuhaojian
LOGNAME=wuhaojian
USER=wuhaojian
...
Copy the code
What are the common environment variables?
- HOME
Effect: Specifies the user’s home working directory. I use a MAC computer, so my HOME is /Users/wuhaojian. If you are using a Windows computer, it should be C:/Users/wuhaojian.
- USER
Action: Specifies the user name. Then of course my computer is Wuhaojian.
- PATH (emphasis)
Function: Specifies the search path of the command. Note: This is in the Windows environment variables panel, called Path. This is to let a lot of small white very headache place, generally download a development software, have to configure the PATH environment variable. It is an environment variable associated with the terminal. If the terminal enters a command, if it is not an internal command, it will go to the PATH to find the file with the corresponding name to execute it. If it cannot find it, an error will be reported. Imagine a situation where you download an NPM package and type NPM install vue on the terminal. When NPM is not an internal or external command and is not an executable program, the environment variables are not configured properly.
- What are internal orders?
Is the computer with some commands, do not download, you can directly use the command. Windows has dir, CD, ifconfig, and so on. Mac has even more, ls, PWD, CD, cat, and so on.
- What are external commands?
CMD on Windows and NPM on Mac. For example, the NPM command is called NPM.
- What is an executable program?
Both internal and external commands mentioned above are executable programs. An executable program is essentially a file that you can run and the system will do something for you. Executable files usually end in.cmd on Windows and can be suffixed, but on Mac they are usually suffixed. Files such as test. TXT and test.json are not executable files. Attempts to execute a non-executable file will result in an error. So how do you solve this problem? If, you of NPM. CMD installed in/Users/wuhaojian/node/NPM. CMD, you have to put the executable file directory/Users/wuhaojian/node is added to the PATH environment variable, this is when you execute NPM, CMD: /Users/wuhaojian/node. CMD: /Users/wuhaojian/node. CMD: /Users/wuhaojian/node
practice
Make your own commands
Once you understand the role of the environment PATH variable, you can easily create your own commands. Assume that your working directory in/Users/wuhaojian/workspace, you create a new file called hello here, and then edit it, as shown in the following code. Then add an executable permission to the Hello file (since the file is created with a default non-executable, this is a file property and can be toggled randomly), chmod +x./hello.
#! /usr/bin/env node
console.log('hi');
Copy the code
Next, put the/Users/wuhaojian/workspace configuration into the PATH environment variable. At this point, restart the terminal, you can type Hello on the terminal, and the terminal will print hi. Note: This is the NodeJS program. First, the computer must have the Node environment configured. Scaffolding vue-CLI comes from this.